I am trying to write a script that will add inputs on click with jQuery/javascript.
But it doesn't work and I cann't find out why. Even console.log() doesn't show anything.
I am no master at jQuery, but it should work I think.
Here is my code
<div id="wrap_inputs">
<?php for($i = 1; $i <= $pocet; $i++) { ?>
<div class="span2" id="span_group<?= $i; ?>">
<label class="control-label" style="display: inline-block; font-size: 11px;"> <?= LANG_CAN_BE_LEFT_OUT; ?></label>
<input type="checkbox" name="can_be_left_out[<?= $i; ?>]">
<input class="span2 m-wrap" name="word[<?= $i; ?>]" type="text" placeholder="<?= LANG_WORD; ?> <?= $i; ?>" style="border: 1px solid black;">
<input class="span2 m-wrap" name="replace[<?= $i; ?>][1]" type="text" id="n<?= $i; ?>_n_slovo" placeholder="<?= LANG_WORDREPLACE; ?>">
<input class="span2 m-wrap" name="replace[<?= $i; ?>][2]" type="text" id="n<?= $i; ?>_n_slovo" placeholder="<?= LANG_WORDREPLACE; ?>">
<input class="span2 m-wrap" name="replace[<?= $i; ?>][3]" type="text" id="n<?= $i; ?>_n_slovo" placeholder="<?= LANG_WORDREPLACE; ?>">
<input class="span2 m-wrap" name="replace[<?= $i; ?>][4]" type="text" id="n<?= $i; ?>_n_slovo" placeholder="<?= LANG_WORDREPLACE; ?>">
<input class="span2 m-wrap" name="replace[<?= $i; ?>][5]" type="text" id="n<?= $i; ?>_n_slovo" placeholder="<?= LANG_WORDREPLACE; ?>">
</div>
<?php } ?>
</div>
Here is my Javascript code
$(document).ready(function(){
var counter = <?= $pocet; ?>;
$("#addButton").click(function () {
var span2_group = $(document.createElement('div')).attr("id", 'span_group' + counter);
span2_group.after().html('<label class="control-label" style="display: inline-block; font-size: 11px;"><?= LANG_CAN_BE_LEFT_OUT; ?>' +
'<input type="checkbox" name="can_be_left_out[' + counter + ']">' +
'<input class="span2 m-wrap" name="word[' + counter + ']" type="text" placeholder="<?= LANG_WORD; ?> ' + counter + '" style="border: 1px solid black;">' +
'<input class="span2 m-wrap" name="replace[' + counter + '][1]" type="text" id="n' + counter + '_n_slovo" placeholder="<?= LANG_WORDREPLACE; ?>">' +
'<input class="span2 m-wrap" name="replace[' + counter + '][2]" type="text" id="n' + counter + '_n_slovo" placeholder="<?= LANG_WORDREPLACE; ?>">' +
'<input class="span2 m-wrap" name="replace[' + counter + '][3]" type="text" id="n' + counter + '_n_slovo" placeholder="<?= LANG_WORDREPLACE; ?>">' +
'<input class="span2 m-wrap" name="replace[' + counter + '][4]" type="text" id="n' + counter + '_n_slovo" placeholder="<?= LANG_WORDREPLACE; ?>">' +
'<input class="span2 m-wrap" name="replace[' + counter + '][5]" type="text" id="n' + counter + '_n_slovo" placeholder="<?= LANG_WORDREPLACE; ?>">'
);
span2_group.appendTo("#wrap_inputs");
counter++;
});
$("#removeButton").click(function () {
if(counter==1){
alert("No more textbox to remove");
return false;
}
counter--;
$("#span_group" + counter).remove();
});
Variable $pocet is number of inputs from first step sent via $_POST[].