0

I want to create input field like given below using java script.Please provide me appropriate solution.

           <input type="text" name="package_location[]"  class="form-control" >

Element is created but When I am posting form I am getting none of the values. I have used this code.

var parinput=document.createElement('input');
            
              parinput.type="text";
 parinput.name="package_location[]";

5
  • Share your try which not worked for you ? Commented Aug 13, 2016 at 11:50
  • var parinput=document.createElement('input'); parinput.type="text"; parinput.name="package_location[]"; Commented Aug 13, 2016 at 11:53
  • Element is created but When I am posting form I am getting a single value Which has been created without using js. Commented Aug 13, 2016 at 11:55
  • There's no such thing like a multivalue input box in HTML. You could use multiple input boxes or split the input string with a separator like a comma or a whitespace. Commented Aug 13, 2016 at 12:02
  • I think what the OP wa trying to say, is that the input box should have multiple attributes, not input values. Commented Aug 13, 2016 at 12:26

3 Answers 3

1

you can create input element but you have to append into body using jquery

var parinput=document.createElement('input');
     $(parinput).attr("type","text");
$(parinput).attr("name","package_location[]");

$('body').append(parinput)
Sign up to request clarification or add additional context in comments.

Comments

0

My java script code is right Only the error is that I have created input field name like package_location[] .And getting it as Package_location the difference of capital P and small p that is the gotcha.

Comments

0

The problem with your code is that you forgot to append the element to the DOM. Using pure JavaScript, simply use the element you made and append it to the body:

var parinput = document.createElement('input'); //Create element
parinput.type = "text"; //Add attribute "text"
parinput.name = "package_location[]"; //Add attribute "name"
document.getElementsByTagName("body")[0].appendChild(parinput) //Append to the first body element found

This solution works without JQuery.

Comments

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.