| | |
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 |
I'm having a problem with adding onClick events to
Here's my code:
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> 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:
HTML Syntax (Toggle Plain Text)
<html> <head> <script type="text/javascript"> function setAJAXAttributes() { var domain = new RegExp("(" + window.location.host + ")|(^\\/.*)|(^[\\w\\d]*\\.[\\w\\d]{2,})", "i"); var links = document.getElementsByTagName("a"); for (var i=0; i<links.length; i++) { if (domain.test(links[i].href)) { links[i].onClick = "getPage('" + links[i].href + "');"; alert("Added onClick: " + links[i].href); } else links[i].target = "_blank"; } } </script> </head> <body> <a href="http://www.daniweb.com">DaniWeb</a><br/> <a href="http://www.example.com/ajax/test.html">Testing absolute path</a><br/> <a href="/ajax/test.html">Testing page relative parent path</a><br/> <a href="test.html">Testing name path</a><br/> <script type="text/javascript">setAJAXAttributes();</script> </body> </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
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
Hi there,
the
the
onClick event, must be all in lowercase onclick :links[i].onclick = "getPage('" + links[i].href + "');"; Assign it as function:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
links[i].onclick = function() { //Callback };
Like this:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
links[i].onclick = function() { getPage( this.href ); };
Dev.Opera — FOLLOW THE STANDARDS, BREAK THE RULES...
Thanks! That worked perfectly. Tried that method earlier, but not in combination with all lowercase
Here is the line that is fixed:
+rep
onclick .Here is the line that is fixed:
JavaScript Syntax (Toggle Plain Text)
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
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
![]() |
Similar Threads
- Dynamically Set CSS Class of an Element (JavaScript / DHTML / AJAX)
- dynamically set onclick event not working :( (JavaScript / DHTML / AJAX)
- How can i add textboxes dynamically in an asp page (ASP)
- ONCLICK or HREF ? (JavaScript / DHTML / AJAX)
- changing from onclick to onsubmit? possible? (JavaScript / DHTML / AJAX)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: RegExp matching a URL
- Next Thread: Multiple File Uploader Script
Views: 784 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for JavaScript / DHTML / AJAX
ajax ajaxcode ajaxhelp animate array box browser bug calendar calling captcha checkbox class content cookies countdown createrange() css cursor design developer div dom dynamic element error event exception explorer explosion file firefox flash focus font form function getelementsbytagname google gwt hide hijack html iframe image images insertbefore internet internet-explorer internetexplorer java javascript javascripthelp2020 javascripts jawascriptruntimeerror jquery js jsp maps marquee modal mp3 mysql onclick onmouseover parameters parent passing perl php player plugins position post refresh regex resize script scroll scrollbar search select servlet session shopping show size starrating stretch text textarea tooltip validate values variables vb.net web webservice window xml





