document.getElementById(); not working in js

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
moglimani
Forum Newbie
Posts: 18
Joined: Thu May 28, 2009 5:54 am

document.getElementById(); not working in js

Post by moglimani »

hi any one their please help me
document.getElementById("id"); not working in js
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: document.getElementById(); not working in js

Post by requinix »

Minimalism does not work in a forum environment.

Keep talking and we'll let you know when you've said enough for us to help.
User avatar
moglimani
Forum Newbie
Posts: 18
Joined: Thu May 28, 2009 5:54 am

Re: document.getElementById(); not working in js

Post by moglimani »

tasairis wrote:Minimalism does not work in a forum environment.

Keep talking and we'll let you know when you've said enough for us to help.

Code: Select all

 function migrate_text() {        place   =   document.getElementById('place'); // <======= here        place.innerHTML="";        var total_count =   document.avg.number_text.value;        loopnext    =   1;                    for(loopvar =   0;loopvar   <   total_count;    loopvar++)  {                place.innerHTML = place.innerHTML +"<tr><td>Input "+loopnext+"</td><td>:</td><td>"+"<input type='text' name='input"+loopvar+"' id='input"+loopvar+"' value=''></td></tr>";                loopnext    =   parseInt(loopnext+1);            }        addnumberbutton =   "<input type='button' value='Add' onclick='addnumbers();'/>";        averagebutton   =   "<input type='button' value='Average' onclick='average();'/>";        place.innerHTML = place.innerHTML +"<tr><td colspan='2' align='right'>"+addnumberbutton+"</td><td align='left'>"+averagebutton+"</td></tr>";    } 
naffan
Forum Newbie
Posts: 6
Joined: Mon Mar 22, 2010 2:39 am

Re: document.getElementById(); not working in js

Post by naffan »

Right code.so find other palce may be wrong.
and your <table id=""> </table> show me(HTML code)
User avatar
moglimani
Forum Newbie
Posts: 18
Joined: Thu May 28, 2009 5:54 am

Re: document.getElementById(); not working in js

Post by moglimani »

naffan wrote:Right code.so find other palce may be wrong.
and your <table id=""> </table> show me(HTML code)

Code: Select all

 <form action="#" name="avg" method="get" id="avg"><table class="table" align="center">    <tr>        <td class="title" colspan="3">Sum $ Average</td>    </tr>    <tr>        <td class="title" colspan="3">&nbsp;</td>    </tr>    <tr>        <td class="title" colspan="3">&nbsp;</td>    </tr>    <tr>        <td class="leftfield">Enter the number of text box you want</td>        <td>:</td>        <td><input type="Text" name="number_text"value="<?php if(isset($_GET["number_text"])) echo $_GET["number_text"];?>" /></td>    </tr>    <tr>        <td colspan="3">&nbsp;</td>    </tr>    <tr>        <td colspan="3">                <input type="Button" value="Migrate" onclick="javascript&#058;migrate_text();" />        </td>    </tr>    <tr>        <td align="center" colspan="3" id="mig">            <div id="place"></div>        </td>    </tr>    <tr>        <td colspan="3">            <div id="sum"></div>        </td>    </tr>                <tr>        <td colspan="3">            <div id="averg"></div>        </td>    </tr>    <tr>            <td align="right" colspan="2"><img align="middle" onclick="javascript&#058;backaction();" src="images/back.jpeg" border="0" width="50" height="50" alt="Back to main form" /></td>    </tr>        </table> </form>
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: document.getElementById(); not working in js

Post by kaszu »

TR is not valid child of DIV. For better performance change innerHTML only once.
Correct:

Code: Select all

function migrate_text() {
    place = document.getElementById('place');
    var html = '<table>';
    var total_count = document.avg.number_text.value;
    loopnext = 1;
 
    for(loopvar = 0;loopvar < total_count; loopvar++) {
        html  += "<tr><td>Input "+loopnext+"</td><td>:</td><td>"+"<input type='text' name='input"+loopvar+"' id='input"+loopvar+"' value=''></td></tr>";
        loopnext = parseInt(loopnext+1);
    }
 
    addnumberbutton = "<input type='button' value='Add' onclick='addnumbers();'/>";
    averagebutton = "<input type='button' value='Average' onclick='average();'/>";
    html += "<tr><td colspan='2' align='right'>"+addnumberbutton+"</td><td align='left'>"+averagebutton+"</td></tr>";
    html += "</table>";
 
    place.innerHTML = html;
}
Post Reply