ThemeForest

jquery addClass problem

286 posts
  • Bought between 1 and 9 items
  • Exclusive Author
  • Has been a member for 4-5 years
  • Microlancer Beta Tester
  • Referred between 1 and 9 users
  • Sold between 1 000 and 5 000 dollars
  • Turkey
AkinGn says

Hey guys,

My aim is changing the class when user clicks to the search form, ie changing the search form’s text color when user wants to enter a search query.. (You know that from many sites, the default text of “Search..” in the box is a pale grey, but when you click the box to enter a search query, the text turns to a black etc to give it a more contrast..) so, I defined a .searchText class in css, and I want jquery to apply it when user clicks the search box. The problem is, when the page is loaded, the class is already added (ie the “Search..” text is already black, when it should be grey), but if you click once in it and click outside, it behaves right. I don’t know what is missing to make it behave right when the page is loaded. My code is:

$(document).ready(function(){
    $('#searchform').click(function() {
                $('#searchform input#s').addClass('searchText');
        });
});

so, what’s wrong here??

348 posts Don't be a HATER!
  • Exclusive Author
  • Has been a member for 3-4 years
  • Elite Author
  • Won a Competition
  • Sold between 100 000 and 250 000 dollars
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Bought between 10 and 49 items
  • Referred between 10 and 49 users
webinpixels says

not click function but try to use focus function :)

good luck

286 posts
  • Bought between 1 and 9 items
  • Exclusive Author
  • Has been a member for 4-5 years
  • Microlancer Beta Tester
  • Referred between 1 and 9 users
  • Sold between 1 000 and 5 000 dollars
  • Turkey
AkinGn says

I solved it lol it was because of a css problem. Actually I tried this because I was trying to get familiar with jquery, I already can do it with pure css, with focus pseudo selector and I think I’m gonna use it. Thanks for your reply, webinpixels :)

by
by
by
by
by