ankur0101 Posted March 7, 2013 Share Posted March 7, 2013 Hi, I have following code > <select name="type"> <option value="all">All</option> <option value="act" selected="">Active</option> <option value="res">Re-signed</option> </select> Is there any way I can add onload event to <select> ? onload is only available for <body>. I am using MVC architecture in PHP. My <body> is located in header.php so I cannot use onload in <body>. What I want to have is when a page loads, my java script function will automatically execute. It would be fine if I put event in any other tag such as <p> Quote Link to comment Share on other sites More sharing options...
trq Posted March 7, 2013 Share Posted March 7, 2013 Most JavaScript frameworks have this (or similar) functionality built in. For instance, jQuery triggers an event when the DOM is ready. http://api.jquery.com/ready. Quote Link to comment Share on other sites More sharing options...
ankur0101 Posted March 9, 2013 Author Share Posted March 9, 2013 Hey trq, how can I execute any existing java script function ? Do I have to write like this > <script> $(document).ready(function () { myFunc(); }); </script> Quote Link to comment Share on other sites More sharing options...
trq Posted March 9, 2013 Share Posted March 9, 2013 Yes. Quote Link to comment Share on other sites More sharing options...
ankur0101 Posted March 10, 2013 Author Share Posted March 10, 2013 Does not work. My function is myFunc(str) and I write as follows : <script> $(document).ready(function () { myFunc(); }); </script> This didnt work so I added parameter i.e. str <script> $(document).ready(function () { myFunc(str); }); </script> Here is how my page looks like <script> function myFunc(str) { ... ... </script> <script> $(document).ready(function () { myFunc(str); }); </script> It does not work at all Quote Link to comment Share on other sites More sharing options...
trq Posted March 10, 2013 Share Posted March 10, 2013 You are of course including the jQuery framework somewhere? Quote Link to comment Share on other sites More sharing options...
ankur0101 Posted March 10, 2013 Author Share Posted March 10, 2013 I found out no jquery is running at all. I have added jquery file. in header. Quote Link to comment Share on other sites More sharing options...
ankur0101 Posted March 10, 2013 Author Share Posted March 10, 2013 (edited) I tried to do $(windows).load(function () { alert('hi); myFunc(str); }); and document.onload=function{ alert('hi'); }; I know this is simple thing but god knows its not working Edited March 10, 2013 by ankur0101 Quote Link to comment Share on other sites More sharing options...
trq Posted March 10, 2013 Share Posted March 10, 2013 Post your actual code. Quote Link to comment Share on other sites More sharing options...
ankur0101 Posted March 10, 2013 Author Share Posted March 10, 2013 I came to know why it is not working. As I said earlier, I am using MVC (Codeigniter here) In Header.php, I am loading calling css and js files along jquery main file. In mainbody.php, I am writing myFunc() and calling that read jquery code. This is why it is not working. I wrote .ready() thing that you said in header.php and it worked fine. Now problem is I dont want to write that in header.php because it is common to all pages. And myFunc() is only called on maonbody.php.This is why I want to write jquery outside <head> i.e. in <body> If jquery does not work, how can I do with java script ? Quote Link to comment Share on other sites More sharing options...
Solution ankur0101 Posted March 10, 2013 Author Solution Share Posted March 10, 2013 (edited) Problem solved, I left --> }); after writing myFunc() Things are working as I wanted. Thanks for your help trq regarding jquery ready. I am bad at java script n jquery. Edited March 10, 2013 by ankur0101 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.