Graham Slick Graham Slick - 1 year ago 85
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 :

var x = 1;
$("#323").val("Step 1");
$("#add").click(function (e) {
$("#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 Source

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

$("#items").append('<div><input type="text" name="input[]" id="323" value="Step' + x + ':" >');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download