Cannot dynamically set onClick

Please support our JavaScript / DHTML / AJAX advertiser: 50% Off 6 Months Web Hosting from 1&1. The World's #1 Host!
Thread Solved

Join Date: May 2009
Posts: 97
Reputation: itsjareds is an unknown quantity at this point 
Solved Threads: 12
itsjareds's Avatar
itsjareds itsjareds is offline Offline
Junior Poster in Training

Cannot dynamically set onClick

 
0
  #1
May 23rd, 2009
I'm having a problem with adding onClick events to <a> tags. The attribute is never added at all, but no errors are shown in the JavaScript console. I'm using Firefox 3.0.10.

Here's my code:
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. function setAJAXAttributes() {
  5. var domain = new RegExp("(" + window.location.host + ")|(^\\/.*)|(^[\\w\\d]*\\.[\\w\\d]{2,})", "i");
  6. var links = document.getElementsByTagName("a");
  7.  
  8. for (var i=0; i<links.length; i++) {
  9. if (domain.test(links[i].href)) {
  10. links[i].onClick = "getPage('" + links[i].href + "');";
  11. alert("Added onClick: " + links[i].href);
  12. }
  13. else
  14. links[i].target = "_blank";
  15. }
  16. }
  17. </script>
  18. </head>
  19. <body>
  20.  
  21. <a href="http://www.daniweb.com">DaniWeb</a><br/>
  22. <a href="http://www.example.com/ajax/test.html">Testing absolute path</a><br/>
  23. <a href="/ajax/test.html">Testing page relative parent path</a><br/>
  24. <a href="test.html">Testing name path</a><br/>
  25.  
  26. <script type="text/javascript">setAJAXAttributes();</script>
  27. </body>
  28. </html>

I made sure that the conditional statement was working correctly by adding an alert to the onClick portion, and the alerts worked fine. However, the onClick event is never added, not on Firefox or IE7.

It also works fine if I change each <a> tag's href to javascript: getPage(); , but I don't want to change what shows in the status bar.
Last edited by itsjareds; May 23rd, 2009 at 9:43 pm.
Mark your post as Solved if you've been helped!

I learn by helping others learn.
If you find one of my posts helpful, don't be afraid to give me a reputation bump :D
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 961
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 134
Featured Poster
essential's Avatar
essential essential is offline Offline
Posting Shark

Re: Cannot dynamically set onClick

 
0
  #2
May 23rd, 2009
Hi there,

the onClick event, must be all in lowercase onclick :

links[i].onclick = "getPage('" + links[i].href + "');";
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 97
Reputation: itsjareds is an unknown quantity at this point 
Solved Threads: 12
itsjareds's Avatar
itsjareds itsjareds is offline Offline
Junior Poster in Training

Re: Cannot dynamically set onClick

 
0
  #3
May 23rd, 2009
Hmm, I tried that, but there's still the same problem. Any other thoughts?
Mark your post as Solved if you've been helped!

I learn by helping others learn.
If you find one of my posts helpful, don't be afraid to give me a reputation bump :D
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 961
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 134
Featured Poster
essential's Avatar
essential essential is offline Offline
Posting Shark

Re: Cannot dynamically set onClick

 
0
  #4
May 23rd, 2009
Assign it as function:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. links[i].onclick = function() { //Callback
  2. };
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 961
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 134
Featured Poster
essential's Avatar
essential essential is offline Offline
Posting Shark

Re: Cannot dynamically set onClick

 
1
  #5
May 23rd, 2009
Like this:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. links[i].onclick = function() { getPage( this.href );
  2. };
Dev.Opera — FOLLOW THE STANDARDS, BREAK THE RULES...
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 97
Reputation: itsjareds is an unknown quantity at this point 
Solved Threads: 12
itsjareds's Avatar
itsjareds itsjareds is offline Offline
Junior Poster in Training

Re: Cannot dynamically set onClick

 
0
  #6
May 23rd, 2009
Thanks! That worked perfectly. Tried that method earlier, but not in combination with all lowercase onclick .

Here is the line that is fixed:
  1. links[i].onclick = function() { getPage(this.href); return false; };

+rep
Mark your post as Solved if you've been helped!

I learn by helping others learn.
If you find one of my posts helpful, don't be afraid to give me a reputation bump :D
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 961
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 134
Featured Poster
essential's Avatar
essential essential is offline Offline
Posting Shark

Re: Cannot dynamically set onClick

 
0
  #7
May 23rd, 2009
Thank's again, itsjareds...
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum


Views: 784 | Replies: 6
Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC