开发者

how to change the text color using jquery or javascript

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


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.


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

 addClass("myClass yourClass");

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜