Sunday, April 28, 2024
 Popular · Latest · Hot · Upcoming
142
rated 0 times [  143] [ 1]  / answers: 1 / hits: 22418  / 15 Years ago, mon, august 3, 2009, 12:00:00

Classic javascript:



var myvar = document.getElementById(abc);
abc.value += test;
abc.value += another test;


Jquery:



$(#abc).val($(#abc).val()+test);
$(#abc).val($(#abc).val()+another test);


Is there a way to make my Jquery prettier, maybe with a hidden += function that I could use? I know that .val() is not an attribute, but I feel there must be a way to make this code more beautiful to look at...



Something like this would be great:



 $(#abc).valueAttribute += test
$(#abc).val().content += test
$(#abc).val().add(test)

More From » jquery

 Answers
44

Since jQuery 1.4, it is possible to pass a function to .val() which gets the current value as second argument:



$(#abc).val(function(i, val) {
return val + test;
});

[#99004] Wednesday, July 29, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
catrinas

Total Points: 587
Total Questions: 100
Total Answers: 105

Location: Rwanda
Member since Thu, Feb 10, 2022
2 Years ago
;