Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to concatenate a variable with string text for displaying as placeholder of an input

I have this script for adding new input field when user click on a link with id of add:

$(document).ready(function() {
            var i = 1;
            $('#add').click(function(){
                i++;
                $('#dyanimc-field').append("<input type='text' name='tag_name[]' value='' class='form-control' id='tagInput' placeholder='Tag name'+i>");
            });
 });

Now this works fine and properly adds a new input field but I want the placeholder of this input to be showing the value of i variable next to the Tag name text.

So the inputs would have these placeholder:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

capture

Therefore I tried this placeholder='Tag name'+i> but this is wrong because it does not return any i variable value!

So how to concatenate a variable with string text for displaying as placeholder of an input correctly?

>Solution :

Try this

$(document).ready(function() {
    var i = 1;
    $('#add').click(function(){
        i++;
        $('#dyanimc-field').append("<input type='text' name='tag_name[]' value='' class='form-control' id='tagInput' placeholder='Tag name " + i.toString() + "'>");
    });
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading