harix
harix

Reputation: 277

why I cant access to value of hidden input element by javascript?

I have an page that I cant use value of hidden input in if clause. and dont print anything in page. I use this javascript command past days and worke but dont work here. my code is:

<script type="text/javascript">

function ch()
{
    alert();
document.write(" brnd = ");
var c=document.getElementById("brnd").value;

document.write(document.getElementById("brnd").value);
document.write(document.forms["br"]["brnd"].value);
}
window.onload=ch();
</script>
</head>

<body >
<form id="br">
    <input type="hidden" id="brnd" value="0000pp"  />
</form>
<p>Page Description.
</p>
<div id="brands" style=""   >
                            <ul style="height:20% !important;width:90% !important;">
                                <li><a href="yat.php" style="color:#000">y.t</a></li>
                                <li><a href="ez.php" style="color:#000">ez</a></li>
                                <li><a href="ami.php" style="color:#000">am</a></li>
                                <li><a href="gr.php" style="color:#000"> group iks</a></li>
                                <li><a href="fr.php" style="color:#000">frtc</a></li>
                                <li><a href="ar.php" style="color:#000">armco</a></li>
                            </ul>
                        </div>

</body>

Where is the problem in your opinion?

=============================================

@Rocket Hazmat: thanks for your note.one problem was place of ch.i move ch to after input and work.but have another problem that i dont know how solved. anyway code work now.thanks all.

Upvotes: 0

Views: 147

Answers (1)

gen_Eric
gen_Eric

Reputation: 227200

window.onload=ch();

This line will run the ch() function and set window.onload to its return value. ch() returns undefined, so you will not be setting onload to anything.

You want to do:

window.onload = ch;

In JavaScript, functions are like other variables. You can just pass them around normally. You use () to call them.

NOTE: document.write should never be used. Using it is most likely your other issue here. Once the page is fully loaded, document.write will destroy your page. It will erase it all and replace it with whatever you passed.

Because of this, your hidden element would be deleted and therefore you can no longer get its value.

Upvotes: 2

Related Questions