Graham Slick Graham Slick - 6 months ago 11
jQuery Question

How to call a variable inside a listener

I'm trying to have displayed each time you click on a button a new textbox with 'Step n' in it : https://jsfiddle.net/askhfu7m/1/

var x = 1;
$("#323").val("Step 1");
$("#add").click(function (e) {
x++;
$("#items").append('<div><input type="text" name="input[]" id="323" value="Step" + x + ":" >');
});


However, it seems like the x is not taken into account in the listener.

Could someone please help me ?

Thanks !!

Answer

Try this (notice the ' before and after x):

$("#items").append('<div><input type="text" name="input[]" id="323" value="Step' + x + ':" >');