Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to do ".value +=" in JQuery?

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")
like image 333
marcgg Avatar asked Aug 03 '09 19:08

marcgg


People also ask

How can get input tag value in jQuery?

To get the textbox value, you can use the jQuery val() function. For example, $('input:textbox'). val() – Get textbox value.

What does VAL () do in jQuery?

val() method is primarily used to get the values of form elements such as input , select and textarea . When called on an empty collection, it returns undefined . When the first element in the collection is a select-multiple (i.e., a select element with the multiple attribute set), .

Can jQuery return value?

You cannot return variable value from jQuery event function.


4 Answers

You could go back to the original DOM element.

 $("#abc").get(0).value += "test";

Otherwise, you'd have to write a plugin

 $.fn.appendVal = function (newPart) {
   return this.each(function(){ $(this).val( $(this).val() + newPart); });
 };

 $("#abc").appendVal("test");
like image 172
Patrick McElhaney Avatar answered Oct 14 '22 11:10

Patrick McElhaney


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";
});
like image 26
Felix Kling Avatar answered Oct 14 '22 11:10

Felix Kling


I've never come across anything like that, doesn't mean it doesn't exist though.

I usually just store val() in a temporary variable and do the manipulation on that, then call val(temp) in a separate line. It spreads the operation out to three or more lines, but it's still more readable than .val(.val() + ""), IMO. It also scales better than +=, if you have a more complicated expression to do to the value.

var temp = $(".abc").val();
temp += "test";
$(".abc").val(temp);
like image 33
Sean Avatar answered Oct 14 '22 10:10

Sean


$() returns a selection; it doesn't return the actual resulting object (although in practice, it simply returns a list of the actual objects). If you want to mutate the object's .value property, you can do this:

$('.abc').each(function(){ this.value += foo; });

If you like, you can create functions that operate on selections, such as .add(), which could be implemented like this:

jQuery.fn.extend({ add: function(k,v) { this[k](this[k]()+v); } });

which can then be used like this:

$('.abc').add('val', foo);

...but I don't think this is any better than using $().each()

like image 34
geocar Avatar answered Oct 14 '22 09:10

geocar