views:

58

answers:

4

Hi, 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>

thanks, kapil

+8  A: 

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.

Pekka
+1  A: 

The input tag doesn't have the onload event.

BrennaSoft
A: 

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.

Roadrunner-EX
+1  A: 

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();">.

bilygates