I’m extremely new to using jQuery/JavaScript, so please don’t judge me if this is a somewhat stupid question :).
What I want to do is create a hidden field with the value of name='link_x', x representing a random number, each time the <div class="delete_button"> containing the same id selector as the class selector of name='link_x' is clicked.
This is what I had in mind, but it doesn’t seem to be working:
My JS:
$(document).ready(function() {
$('div.delete_button').click( function() {
var buttonId = $(this).attr('id');
var inputVal = $('input').hasClass(buttonId).val();
var num = $('.clonedInput').length;
var newNum = new Number(num + 1);
var newElem = $('#hidden' + num).clone().attr('id', 'input' + newNum);
newElem.children(':first').attr('name', 'link[' + newNum + ']').val(inputVal);
$('#input' + num).after(newElem);
$(this).css('display','none');
});
});
My HTML:
<form action="delete-func.php" method="post">
<div id="hidden1" class="clonedInput">
<input name="link[1]" type="hidden" value="http://www.google.com/" />
</div>
<input class="1" name="del_link" type="text" value="http://www.euieuek.com" /><div id="1" class="delete_button" style="color: red; cursor: pointer;">X</div>
<input type="submit" value="Delete from records!" />
</form>
Any edits, answers, comments would be very much appreciated! Please ask for more information if you think what I’ve currently given is obscure!
🙂
Change
to