JavaScript How to create text box dynamically from javascript create text field dynamically javascript,create text file dynamically in

I am trying to create textboxes inside <div id="screens"> </div>. I have a drop down menu for which I select the number of textboxes to be created.

The JavaScript code I've written here is not working:

code:

function create(param) {
    document.getElementById("screens").innerHTML="";          
    for(var i = 0; i < param; i++) {
        alert(i);
        document.write('<input type="text" name="Fname">');
    }
}

This is adding textboxes by clearing all contents of the current page, but I want the textboxes to be added to <div id="screen"> </div>. How can I do this?

Answer:1

Try something like this:

function create(param) {
    var s= "";
    for(var i = 0; i < param; i++) {
        s+= '<input type="text" name="Fname">'; //Create one textbox as HTML
    }
    document.getElementById("screens").innerHTML=s;
}
Answer:2

Since you've tagged jQuery in your question, I'll assuming that you're using jQuery.

function create(param) {
    'use strict';

    var i;

    $("#screens").empty();

    for(i = 0; i < param; i += 1) {
        $('#screens').append('<input type="text" name="Fname">');
    }
}

jsFiddle

A pure JavaScript implementation would look like this:

function create(param) {
    'use strict';

    var i, target = document.getElementById('screens');
    target.innerHTML = '';

    for(i = 0; i < param; i += 1) {
        target.innerHTML += '<input type="text" name="Fname">';
    }
}

jsFiddle

Answer:3
function create(param) {
    var screens = document.getElementById('screens'),
        innerHTML = '',
        i;

    for (i = 0; i < param; i += 1) {
        alert(i);
        innerHTML += '<input type="text" name="Fname">';
    }

    screens.innerHTML = innerHTML;
}
Answer:4
function create(param) {
    var target = document.getElementById('screens'),
        oFrag=document.createDocumentFragment();

    target.innerHTML = '';

    for (var i = 0; i < param; i++) {
        var iptNode = document.createElement("input");
        iptNode.setAttribute("type", "text");
        iptNode.setAttribute("name", "Fname");
        oFrag.appendChild(iptNode);
    }

    target.appendChild(oFrag);
}
Answer:5

I'm using gridster to make a grid of links. The link should work normal when click on it. Problem is it's also get clicked after dragged. How can I stop it from being clicked after dragged? Please ...

I'm using gridster to make a grid of links. The link should work normal when click on it. Problem is it's also get clicked after dragged. How can I stop it from being clicked after dragged? Please ...

  1. prevent click while dragging
  2. jquery prevent click while loading
  3. jquery prevent click while animating

I'm using angularjs 1.3.2. How to get clipboard data on paste event? This question is like: "Paste" event in Angular [ngPaste] But in this question, the angularjs version is 1.2. And this ...

I'm using angularjs 1.3.2. How to get clipboard data on paste event? This question is like: "Paste" event in Angular [ngPaste] But in this question, the angularjs version is 1.2. And this ...

  1. paste clipboard data javascript
  2. paste clipboard data vba
  3. paste clipboard data in c#
  4. paste clipboard data to excel c#
  5. paste clipboard data
  6. clipboard data not suitable for paste

My replace all function is as below, it is in commonHelper.js file exports.replaceAll = function (find, replace, str) { return str.replace(new RegExp(find, 'g'), replace); } Then I do following ...

My replace all function is as below, it is in commonHelper.js file exports.replaceAll = function (find, replace, str) { return str.replace(new RegExp(find, 'g'), replace); } Then I do following ...

  1. search replace javascript
  2. location.search.replace javascript
  3. search replace string javascript
  4. search and replace javascript regex
  5. regex search replace javascript
  6. javascript search replace all
  7. javascript search replace text
  8. javascript search replace array
  9. search and replace html javascript
  10. str search and replace javascript
  11. javascript search replace characters

I'm using a content-script in a chrome extension that I'm writing. I included geolocation in my list of permissions, but on every webpage I still get asked if I want to share my location. I thought ...

I'm using a content-script in a chrome extension that I'm writing. I included geolocation in my list of permissions, but on every webpage I still get asked if I want to share my location. I thought ...