By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 941 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

How to use windows onload fuction twice in a same page

P: 5
I have a java script which I used for passing check boxes values to text box. But it does not work if i use it twice in a same page. How can I use it many times in a same page?


Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. <script type="text/javascript">
  4.  
  5.  
  6.  
  7.  
  8. window.onload=function(){
  9.     var textbox = document.getElementsByName("textname")[0];
  10.     var checkboxes = document.getElementsByName("checkboxname");
  11.     for (var i = 0; i < checkboxes.length; i++) {
  12.         var checkbox = checkboxes[i];
  13.         checkbox.onclick = (function(chk){
  14.             return function() {
  15.                 var value = "";
  16.                 for (var j = 0; j < checkboxes.length; j++) {
  17.                     if (checkboxes[j].checked) {
  18.                         if (value === "") {
  19.                             value += checkboxes[j].value;
  20.                         } else {
  21.                             value += "," + checkboxes[j].value;
  22.                         }
  23.                     }
  24.                 }
  25.                 textbox.value = value;
  26.             }
  27.         })(checkbox);
  28.     }
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37. </script>
  38.  
  39.  
Sep 27 '13 #1

✓ answered by Dormilich

just replace window.onload=function(){} by window.addEventListener("load", function(evt) {})

Share this Question
Share on Google+
4 Replies


Dormilich
Expert Mod 5K+
P: 8,639
itís simple, you canít.

you have to use Event Listeners to attach more than one handler to a single event:
Expand|Select|Wrap|Line Numbers
  1. window.addEventListener("load", function1);
  2. window.addEventListener("load", function2);
  3. window.addEventListener("load", function(evt) { /* ... */ });
Sep 27 '13 #2

P: 5
hey can you help me by writing an event listener because i am beginner. I copied the java script code from somewhere, i don't know where i add even listener code.
Sep 27 '13 #3

Dormilich
Expert Mod 5K+
P: 8,639
just replace window.onload=function(){} by window.addEventListener("load", function(evt) {})
Sep 27 '13 #4

P: 5
a million dollar thanks to you. It finally worked.
Sep 28 '13 #5

Post your reply

Sign in to post your reply or Sign up for a free account.