views:

54

answers:

3

I want to change css class of an element on certain event in jquery. eg.

            focusin: function()
                {
                    if(this.val()=="First Name")
                        $(this).find("span ").css
                }

$(this).find("span which contains _set").class= mycssclass here I want to change the CSS class of span element whose id contains "_set" as a part of substring in class name and also want to change the text i.e. InnerHtml property of javascript of this span element

+1  A: 

.removeClass() + .addClass() should do the trick

Derek Adair
+2  A: 

something like this

$("span[id*='_set']").toggleClass("newcssclass").text("new text").

regards

Sebastian Marcet
+1  A: 

where does it contains "_set" in the ID ,CLASS or in the innterHTML ?

to look for some string in the innerHTML you need to use something like this:

$("span:contains('_set')").toggleClass("yourcustomclass").html("blabla")

if the "_set" is in the ID or in the class use it like "hworangdo" told you. In your case:

$("span[id*='_set']").toggleClass("yourcustomclass").text("new text").

why are you using "$(this)" before the ".find()" in your example?

meo
@david: thx for xplanation. I m using it on textbox's focus and blur event
Shantanu Gupta
so you can not use $(this).find('span') because the span is not inside your textbox.Where is the span you are looking for? To make your script faster you could be more specific when selecting your span. The example i have posted is gonna by pretty slow an a page with a lot of content.
meo
@david thx for help. It is a sibling of textbox, i fixed up this issue after u pointed out my mistake
Shantanu Gupta