0

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.

11
  • 1
    This may just be a typo here, but document.forms[br"]["brnd"] is a syntax error. You're missing a ". Commented Feb 5, 2016 at 22:01
  • 2
    Where is hidden input ? Commented Feb 5, 2016 at 22:02
  • I'm pretty sure there are errors in the console. Those errors should help you find out what the issue is. Commented Feb 5, 2016 at 22:05
  • 1
    @harix: codepen.io/anon/pen/OMBmEJ it is working fine for me. Commented Feb 5, 2016 at 22:10
  • 1
    @Pbk1303: Because you changed the order of the JS code in relation to the HTML code. Commented Feb 5, 2016 at 22:12

1 Answer 1

2
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.

Sign up to request clarification or add additional context in comments.

4 Comments

function execute well and show alert but dont display value of input
@harix: What do you mean?
dont display value input in page
@harix: It's displayed in the console. If you want to put it on the page, you'll need to append it to the DOM. P.S. document.write is not the correct way to do this.

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.