Solved

How to replace img onclick event using javascript

Posted on 2008-10-16
13
3,086 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 8

Accepted Solution

by:
MatthiasVance earned 500 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

830 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