5

I want to use javascript to create <input/> tags with a dynamic id (name attribute will be the same) depending on the order in which they appear.

I have the first <input/> tag in plain html and a div that should append a new <input/> with an incremented id when clicked:

<input type="text" name="1" id="1" />
<div class="add_new" onClick="add_new_input()">+</div>

Now, the javascript needs to count the amount of <input/>s currently being displayed (count) and use that amount to generate a dynamic id (count+1).

Therefore if the <div class="add_new"><.. is clicked twice, the output should be as follows:

<input type="text" name="1" id="1" />
<input type="text" name="2" id="2" />
<input type="text" name="3" id="3" />

If I append the new <input/> tag in my form using jquery's append(), would this add to the previously appended <input/>s? Or would I need to append one <input/>, then two, then three, etc?

Also, how can I use javascript to count the amount of <input/>s currently being displayed?

1
  • 1
    you can use $(':input:text').size() to count the text input control Commented Nov 15, 2013 at 5:40

8 Answers 8

9

I had used script which is fit in your requirement .

Html:

<h2><a href="#" id="addScnt">Add Another Input Box</a></h2>

<div id="p_scents">
    <p>
    <label for="p_scnts">
        <input type="text" id="p_scnt" size="20" name="p_scnt" value="" placeholder="Input Value" />
    </label>
    </p>
</div>

Javascript :-

  $(function () {
    var scntDiv = $('#p_scents');
    var i = $('#p_scents p').size() + 1;

    $('#addScnt').live('click', function () {
    $('<p><label for="p_scnts"><input type="text" id="p_scnt" size="20" name="p_scnt_' + i + '" value="" placeholder="Input Value" /></label> <a href="#" id="remScnt">Remove</a></p>').appendTo(scntDiv);
    i++;
    return false;
    });

    $('#remScnt').live('click', function () {
    if (i > 2) {
        $(this).parents('p').remove();
        i--;
    }
    return false;
    });
  });

working demo

Sign up to request clarification or add additional context in comments.

Comments

4

Try like

function add_new_input() {
   var last_id = $('input[type="text"]:last').attr('id');
   last_id++;
   $('input[type="text"]:last').append('<input type="text" name="'+last_id+' id="'+last_id+'">');
}

1 Comment

Ok, so javascript would remember the last appended <input/> after every click?
1

Try this

function add_new_input(){
   var lastControlId = $('input:text:last').attr('id');
   if(!lastControlId){
    lastControlId = parseInt(lastControlId,10) + 1;
    $('#containerDiv').append('<input type="text" name="'+ lastControlId +' id="'+ lastControlId +'">');
  }
}

Comments

1

WORKING FIDDLE

jQuery('#select').change(function () {
var val = jQuery(this).val();
var innerhtml = '';
for (var i = 0; i < val; i++) {
    innerhtml += "<input type='text' id='" + (i + 1) + "' name='" + (i + 1) + "'size=50>   </br>";
}
jQuery('#textbox_div').html(innerhtml);
});

1 Comment

Nice solution! Not precisely what I was looking for but may use this elsewhere!
1
<body>
<div>Hello world mggp</div>
<div id="li">
    
</div>
<button type="button" onclick=add() class="btn btn-light">+addtodo</button>
<script>
var i =1;
function add() {

document.getElementById('li').innerHTML+=`<div 
id="li${i}">added</div>`;
i++

 }
</script>
</body>

1 Comment

Please describe your code and say somethings about your solution.
0
$( "input" ).each(function( index ) {
  $(this).attr('id', 'item-' + index);
});

This should alter all inputs with id of "item-[index number]".

But you should throw in .class identification to selector so this doesn't apply to all.

Comments

0

using plain javascript

<script language="javascript">
    var x=1;
    function add_new_input(){
        x++;
        var input=document.createElement('input');
        input.setAttribute("id",x);
        input.setAttribute("type","text");

        document.getElementById("holder").appendChild(input);

    }
</script>

html is below

<div id="holder">
<input type="text" name="1" id="1" />
</div>
<div class="add_new" onClick="add_new_input()" >+</div>

Comments

0
var index = 1;


function add_fields() {

    index++;
    var objTo = document.getElementById('room_fileds')
    var divtest = document.createElement("tr");
    divtest.innerHTML = ("<tr><td class='slno'>" + index + "</td><td><input type='text'></td><td><button class='remove_button' onclick='myfn()'>remove</button></td></tr>");
    objTo.appendChild(divtest);
     $(".slno").each(function(index, element) {
            $(element).text(index + 1);

       });

}

function myfn() {
        $(".remove_button").parents('tr').first().remove();
        $(".slno").each(function(index, element) {
            $(element).text(index + 1);

        });
}

1 Comment

<button onclick="add_fields()">add input </button> <table id="room_fileds"> <tr> </tr> </table>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.