I want change name attribute in <input> element, I have in the here several name string[0][], now i want after click on link add change 0 in they and replace 1 instead all 0. How is it by Jquery?
DEMO: http://jsfiddle.net/2hXk5/
I try as but don’t work for me:
<div class="ch">
<p>
<input type="checkbox" name="cDA[0][]">
<input type="checkbox" name="cDA[0][]">
<input type="checkbox" name="cDA[0][]">
<input type="checkbox" name="cDA[0][]">
</p>
<p>
<input type="checkbox" name="cFE[0][]">
<input type="checkbox" name="cFE[0][]">
<input type="checkbox" name="cFE[0][]">
<input type="checkbox" name="cFE[0][]">
</p>
<p>
<input type="checkbox" name="cYG[0][]">
<input type="checkbox" name="cYG[0][]">
<input type="checkbox" name="cYG[0][]">
<input type="checkbox" name="cYG[0][]">
</p>
<a href="" class="adding">Add</a>
</div>
$('a.adding').live('click change', function (e) {
e.preventDefault();
$clone = $(this).closest('.ch').clone().insertAfter('.ch');
$clone.find('input[type="checkbox"]:contains("0")').prop('1');
})
If I understand you correctly, you want to use
.attr()with a callback function:UPDATE: Here’s a complete version which will do everything you seem to actually want. It uses
str.match(/\d+/)to extract the FIRST number-string in thenameattribute, converts it to an integer, then replaces it with the next integer usingstr.replace. Note theinsertAfterpart was updated so it only inserts after the LASTdiv.ch:http://jsfiddle.net/mblase75/3SGNw/