Home how to pass string variable of JavaScript to html form input,
Reply: 2

how to pass string variable of JavaScript to html form input,

A.F.R.N
1#
A.F.R.N Published in 2018-01-13 08:12:49Z

Like if I have element of array name[1]='ahmad' and I want it to be posted in html form.

I used

document.getElementById('n').value=name[1];

but it's not posting.

Form has:

<input type="text" id="n" readonly="true">

i want to fill array by user and show these values in input readonly boxes on web page. full code is:

<!dochtml html>
<title>Arrays of Js</title><!--different methods of using declaring arrays-->
<head>
<h3>Arrays usage by AFRN</h3>
    <script>
    alert('hey');
    var names=[];
    //var n = prompt('enter name');
        for(var i=0; i<=4; i++){
            names[i]=prompt('enter name: ');
        }
    /*var ids=['r1','r2','r3','r4','r5']
        for(var i=0; i<=4; i++){
            document.getElementById(ids).value=name[i];
        }*/
    document.getElementById("r1").value=names[0];
    document.getElementById("r2").value=names[1];
    document.getElementById("r3").value=names[2];
    document.getElementById("r4").value=names[3];
    document.getElementById("r5").value=names[4];
    </script>
        <noscript>JS not supported</noscript>
</head>
<body bgcolor="yellow">
    <form name="arryForm">
        <input type="text" id='r1' name="a1" readonly="true"> 
        <input type="text" id='r2' name="a2" readonly="true"> 
        <input type="text" id='r3' name="a3" readonly="true"> 
        <input type="text" id='r4' name="a4" readonly="true"> 
        <input type="text" id='r5' name="a5" readonly="true"> 
    </form>
</body>
</html> 
cstls
2#
cstls Reply to 2018-01-14 11:56:56Z

If you move your <script> tag with its contents to the bottom just before the closing </body> tag and it will work.

Muhammad Usman
3#
Muhammad Usman Reply to 2018-01-13 09:38:38Z

You can try this code ! Place your javascript code below the input tag in body and you cannot use the variable name 'name' , I think this is not allowed in javascript because I am also facing problem with the varaible name 'name' so I am using '_name' .

var _name = ["usman","ahmed"];
document.getElementById("n").value = _name[1];
<input type="text"  id="n" readonly="true"/>

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.298278 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO