Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Dynamically changing a javascript onclick value

Posted on 2008-11-11
5
Medium Priority
?
1,032 Views
Last Modified: 2012-05-05
I have tried unsuccessfully to dynamically change the onlick value that is currently in an <a> tag.  It seems like the code below should work but I get an error at: thisoc =  allLinks[i].onclick;

Thanks.
---HTML---
<a href="http://www.mysite.com/somepage.php?id=381" onclick="function1();">Some text to click</a>
 
---Javascript in my function---
 
    allLinks = document.getElementsByTagName('a');
	for (var i = 0; i < allLinks.length; i++) {
	    thislink = allLinks[i].href;
            thisoc =  allLinks[i].onclick;  //This statement produces an error
 
            if(thislink.indexOf('somepage.php') > 0) {
              //Need to insert our own function call in the onclick part
              thisoc.replace(/function1/gi, "alert('zvc');function1");
              allLinks[i].onclick = thisoc;
            }
	}

Open in new window

0
Comment
Question by:Thread7
  • 3
5 Comments
 
LVL 7

Expert Comment

by:alexpercsi
ID: 22937218
The problem is that calling thisoc=allLinks[i].onclick will not produce a string value in thisoc, it will produce a reference to the function.

You can try something like this:

allLinks[i].onclick=function(){alert('zvc');function1();}

Open in new window

0
 
LVL 15

Expert Comment

by:quincydude
ID: 22937267
You may like to check out this code about the technique
<html>
<head>
<script language="javascript">
function saybye() 
{
alert("bye!");
}
 
function change()
{
var me = document.getElementById('field2');
alert(me.attributes['onclick'].value);
me.onclick = function() {saybye()};
}
</script>
</head>
<body>
 
Field2: <input type="text" id="field2" onclick="alert('hi');">
<br /><br />
Click the button below to change method of onclick.
<br />
<button onclick="change();">Change</button>
 
 
</body>
</html>

Open in new window

0
 
LVL 1

Author Comment

by:Thread7
ID: 22939497
Hmm, I don't think the statement

thisoc=allLinks[i].onclick;

even creates a reference to the function.  My code stops running at that line.  I tried putting this line instead but it also stops my code at this point.

thisoc = allLinks[i].attributes['onclick'].value;
0
 
LVL 7

Accepted Solution

by:
alexpercsi earned 2000 total points
ID: 22941363
Have you tried the line I had suggested?

As an alternative, you could try downloading firebug, a developer plugin for mozilla firefox, and using it to determine the specific error that occurs.

You can get it here:
https://addons.mozilla.org/en-US/firefox/addon/1843

Don't forget to enable it before running your code by clicking the bug icon on the lower right corner of your browser and checking the Console and Script checkboxes.

Best Regards,
Alex Percsi
0
 
LVL 7

Expert Comment

by:alexpercsi
ID: 22941442
thisoc = allLinks[i].attributes['onclick'].value; will not work because the attributes collection is not supported correctly on most browsers.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question