First of all, excuse me for my bad english.
I've tried various methods but haven't had any luck as of yet.
I'm returning a series of objects with .each()method.
I'd like to populate the "value" attribute of a input field with each value from the object. I didnt find a way to do the same thing as PHP .=
Any idea would be great!
Here's my code :
$.each($("input[type='checkbox']:checked"), function(){
var data = $(this).parent().parent().find("td:eq(1)");
$("#login").val(data.text());
})
var c = '';
$.each($("input[type='checkbox']:checked"), function(){
var data = $(this).parent().parent().find("td:eq(1)");
c += data.text();
})
$("#login").val(c);
You could also have used this in the loop :
$("#login").val($("#login").val(c)+data.text());
but I personally prefer to minimize the changes of the DOM.
Note also that using traversals like parent().parent()
lead to maintenance problems. In your case you could probably use
var data = $(this).closest('tr').find("td:eq(1)");
so that the code won't break if a span
or any other elements wraps your input in the future.
You can use map(). You can pass the delimiter
character to join() to separate the alues.
text = $("input[type='checkbox']:checked").map( function(){
return $(this).parent().parent().find("td:eq(1)");
}).get().join();
$("#login").val(text);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With