Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
92
rated 0 times [  93] [ 1]  / answers: 1 / hits: 58154  / 12 Years ago, mon, march 19, 2012, 12:00:00

I've no idea why this isn't working, but I'm sure it's a very common bit of Javascript. My syntax is clearly lacking, and I don't know why:



<input type=text value=search id=search name=q onclick=javascript:if($(this).val() == 'search') {$(this).val() = '';} return false; />

More From » jquery

 Answers
12

Your issue is with this line of code:



$(this).val() = ''


The proper way to set a value in jQuery is:



$(this).val(Your value here);


In addition, inline JS is never a good idea. Use this instead:



$(#search).on(click, function() {
if ($(this).val() == search)
$(this).val()
});​


Here's a working fiddle.



References: jQuery .val()


[#86746] Sunday, March 18, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
longd

Total Points: 616
Total Questions: 110
Total Answers: 101

Location: Andorra
Member since Sat, May 27, 2023
1 Year ago
;