Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to replace img onclick event using javascript

Posted on 2008-10-16
13
Medium Priority
?
3,143 Views
Last Modified: 2012-05-05
Hi every one i have following code:

and i need to replace:

onclick="openw('http://www.google.com/',0,0,0,0,0,1,1,900,900,200,1,1);" value="openw"

to this using window.onload event

onclick="openw('http://www.yahoo.com/',0,0,0,0,0,1,1,900,900,200,1,1);" value="openw"

Can some one write a quick sample?
<td rowspan="3" valign="top"><a href="#"><img src="http://www.mysite.com/images/template_05.gif" width="172" height="45" border="0" onclick="openw('http://www.google.com/',0,0,0,0,0,1,1,900,900,200,1,1);" value="openw"/></a></td>

Open in new window

0
Comment
Question by:olivest
  • 6
  • 5
  • 2
13 Comments
 
LVL 7

Expert Comment

by:ASPSQLServerCOM
ID: 22731259
http://www.quirksmode.org/js/events_advanced.html

element.addEventListener('click',startDragDrop,false)
0
 

Author Comment

by:olivest
ID: 22731464
But this solution will apply to all onclick events.I need it for only this one particular.
0
 
LVL 8

Expert Comment

by:MatthiasVance
ID: 22731530
I hope this helps you out.

Kind regards,

Matthias Vance
<script language="JavaScript">
	function openw(url, arg) {
		alert("URL: " + url);
	}
</script>
<td rowspan="3" valign="top"><a href="#"><img src="http://www.google.com/intl/en_ALL/images/logo.gif" border="0" onclick="openw('http://www.google.com/',0);" value="openw"/></a></td>
<script language="JavaScript">
	var img = document.getElementsByTagName("img");
	for(var i = 0; i < img.length; i++) {
		img[i].setAttribute("onclick", img[i].getAttribute("onclick").replace("google", "yahoo"));
	}
</script>

Open in new window

0
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.

 
LVL 8

Accepted Solution

by:
MatthiasVance earned 2000 total points
ID: 22731570
Sorry, I read your reply too late, use this code to only pick the ones with value of "openw".

<script language="JavaScript">
	function openw(url, arg) {
		alert("URL: " + url);
	}
</script>
<td rowspan="3" valign="top"><a href="#"><img src="http://www.google.com/intl/en_ALL/images/logo.gif" border="0" onclick="openw('http://www.google.com/',0);" value="openw"/></a></td>
<script language="JavaScript">
	var imgs = document.getElementsByTagName("img");
	for(var i = 0; i < imgs.length; i++) {
		var img = imgs[i];
		if(img.getAttribute("value") == "openw") {
			img.setAttribute("onclick", imgs[i].getAttribute("onclick").replace("google", "yahoo"));
		}
	}
</script>

Open in new window

0
 
LVL 7

Expert Comment

by:ASPSQLServerCOM
ID: 22731975
var oLink = document.getElementById('imgTest');

oLink.onclick = new Function("javascript:openw('http://www.yahoo.com/',0,0,0,0,0,1,1,900,900,200,1,1);");

0
 
LVL 7

Expert Comment

by:ASPSQLServerCOM
ID: 22731992

<td align="left"><a href="#">
                <img src="http://www.mysite.com/images/template_05.gif" width="172" height="45" 
                    border="0" 
                    onclick="openw('http://www.google.com/',0,0,0,0,0,1,1,900,900,200,1,1);" 
                    value="openw" name="imgTest" id="imgTest"/></a></td>

Open in new window

0
 

Author Comment

by:olivest
ID: 22732007
Well alert box pop's up with old url it doesn't change it.Maybe i need to add it all to function and run window.onload event?
0
 
LVL 7

Expert Comment

by:ASPSQLServerCOM
ID: 22732035
YES


window.onload = function () {
 
 
var oLink = document.getElementById('imgTest');
 
	
oLink.onclick = new Function("javascript:openw('http://www.yahoo.com/',0,0,0,0,0,1,1,900,900,200,1,1);");
}

Open in new window

0
 

Author Comment

by:olivest
ID: 22732071
seams to work now.Done as i said.Added a function and onload event.
0
 

Author Comment

by:olivest
ID: 22732112
ASPSQLServerCOM: there is no element id on image so you can't pick it up.Only by tag's like Matthias suggested.Please pay attention to source code next time "ASPSQLServerCOM".
0
 
LVL 7

Expert Comment

by:ASPSQLServerCOM
ID: 22732148
thats why i gave you code earlier
<td align="left"><a href="#"> <img src="http://www.mysite.com/images/template_05.gif" width="172" height="45" border="0" onclick="openw('http://www.google.com/',0,0,0,0,0,1,1,900,900,200,1,1);" value="openw" name="imgTest" id="imgTest"/></a></td>  
0
 

Author Comment

by:olivest
ID: 22732241
Read again!!! There is no ID assigned to the image i needed alternative way and here it is.Thanks to MatthiasVance:
<td rowspan="3" valign="top"><a href="#"><img src="http://www.google.com/template_05.gif" width="172" height="45" border="0" onclick="openw('https://www.google.com',0,0,0,0,0,1,1,900,900,200,1,1);" value="openw"/></a></td>
 
 
<script language="JavaScript">
function rpl (){
        var imgs = document.getElementsByTagName("img");
        for(var i = 0; i < imgs.length; i++) {
                var img = imgs[i];
                if(img.getAttribute("value") == "openw") {
                        img.setAttribute("onclick", imgs[i].getAttribute("onclick").replace("https://www.google.com", "http://www.yahoo.com"));
                }
        }
        }
        window.onload=rpl;
</script>

Open in new window

0
 
LVL 7

Expert Comment

by:ASPSQLServerCOM
ID: 22732400
I tried the solution you gave
and it gives javascript error
object doesnt support this property or method
but if it has resolved your problem I am happy on it, and we are here for providing solution, I provided the solution considering there is only one IMG, and no where in question it has been said that you want to assign it to all the images on the page, so the solution has been provided as per the question, better provide the total detail so its easy for a person to assist you in getting the solution
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

The most up-to-date version of this article is on my Blog https://iconoun.com/blog/
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

877 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