0

I have a two items(rows) in the list box

I love to work with jquery i love to work with javascript

I have textbox and button when I enter love in the text box when I click button i need to loopthrow this list box to find the text love and change that love text color? to yellow.

thanks

5
  • possible duplicate of how to change the text color using using jquery Commented Sep 27, 2011 at 20:21
  • 1
    and what is your current code? Commented Sep 27, 2011 at 20:23
  • This is my current code where I am trying to find the text. $("#lstCodelist option").each(function () { //I dont know what I have to write here? }); Commented Sep 27, 2011 at 20:23
  • 1
    Most browsers don't allow for styling of the options of a select box. It's entirely possible that what you're asking for isn't achievable without using a styled ul (or ol) in place of a select. Commented Sep 27, 2011 at 20:26
  • i think option is the name/id of text box:-) Commented Sep 27, 2011 at 20:31

2 Answers 2

1

first of all, jQuery is javascript, it's a library written in javascript.

So, If I understand your problem, you have 3 interactive elements on your page:

  • a list box containing a list of words
  • a text field for the user to enter a word
  • a button for the user to click when he has written the text.

And you want the option to change color when the user clicks the button. the code for thsi would be something like this:

$("#mybutton").click(function(){
  var text = document.getElementById("mytextinput").value
  $("#lstCodelist option").each(function (){
   if(this.text()===text)
      this.css('color':'yellow');
  });

});

this is what happens:

  • line 1: I define a click handler when the button gets clicked.
  • line 2: I get the text from inside the textbox, I use getElementById to avoid the overhead of using jQuery for something that simple
  • line 3: I loop over each of the items in the list.
  • line 4: if the string in the textbox equals the text inside the option:
  • line 5, change the css property of the list option.

So no, this is not affecting the text, it only edits the css.

Sign up to request clarification or add additional context in comments.

1 Comment

is this going to split each string in and will change the text for that string it self?
0

for changing text box color, you can add class to the element

 addClass("myClass yourClass");

http://api.jquery.com/addClass/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.