How to input variable inside regex javascript?

How to change color of a text as an input using javascript?

  • My assignment Count the number of times a specified letter (an input value) occurs in a sentence (another input).Set the color as defined above in "Color". You must use "document.write" to display the results. Im a beginner, Javascript and i need this ASAP, thks So far i got this code: doesnt work properly <script language="JavaScript"> <!-- function Count(char, sent) { pos = 0; count = -1; i = -1; while (pos != -1) { pos = sent.indexOf(char, i + 1); count++; i = pos; } if (count >= 2) { document.test.occur.value = count; } else { document.test.occur.value = count; } <form name="test"> <p>Enter a character: <input name="char" type="text"><br> Enter the sentence: <input name="sent" type="text"></p> <p><input type="button" value="Click Me" onClick="Count(document.test.char.value, document.abu.sent.value);"> Occurrence: <input name="occur" type="text">

  • Answer:

    First of all, javascript must be enclosed between <script> tags. You have only an opening <script> tag. Also, the language attribute is "text/javascript". I don't know exactly what you mean by "change the color" To change the font color, use document.getElementById("object id").style.color = "red"; To change the background color use document.getElementyById('object id').style.backgroundColor = "red"; To add a colored border use document.getElementById("object id").style.border = "1px solid red"; For example, to change the color of the text in an input with an id of "test" to green, type <script type='text/javascript'> document.getElementById("test").color = "green"; </script>

abu n at Yahoo! Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.