I am trying this
var newNum = some new number;
$("#myDiv fieldset:last").find(':input').attr('name', $(this).attr('name') + newNum);
If an input field in myDiv is 'firstName' i want to rename it 'firstName15'开发者_JAVA技巧 may be!
How can i achieve this?
Thanks.
Try using .each()
$("#myDiv fieldset:last").find(':input').each(function(i, e) {
$(e).attr('name', $(e).attr('name') + newNum);
});
Of course if you are going to be incrementing the counter in the operation make sure you account for that, but if you are only doing one assignment, you can create a variable containing the element you are selecting and operate on that otherwise $this
in the context above does not refer the element you are selecting.
var $input = $("#myDiv fieldset:last").find(':input');
$input.attr('name', $input.attr('name') + newNum);
this
is not the input element in that context.
EDIT: I may have misunderstood your question slightly. If your intent is to work on multiple input elements and not just one, use @Quintin's solution with each
.
精彩评论