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

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

user1763
1#
user1763 Published in June 24, 2018, 3:23 am

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> 
You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO