1

I am trying to invoke a script function on my MVC view as follows, but it's not happening. What may be the reason behind it?

<input type="text" id="hdnTemp" value="xyz" onload="test();" />                    
                    <script type="text/javascript">
                      function test() {
                        alert();
                      }
                    </script>

4 Answers 4

8

input elements do not have an onload event.

W3C Reference

The onload event occurs when the user agent finishes loading a window or all frames within a FRAMESET. This attribute may be used with BODY and FRAMESET elements.

You could use the body's onload event, or another event in your input like onclick or onchange, depending on what you want to do.

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

Comments

1

The input tag doesn't have the onload event.

Comments

1

The onload event is only for body and frameset elements. See here for more info: http://www.w3.org/TR/REC-html40/interact/scripts.html#events .

If you want the function to execute after the page loads, you can use <body onload="test();">.

If you want to execute it after the user types something, you can use <input ... onchange="test();">.

Comments

0

It's been a while since I worked in Javascript, but I'm pretty sure you have to declare the function before calling it. So, your 'onload()' doesn't know what it's calling.

Comments

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.