Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to replace img onclick event using javascript

Posted on 2008-10-16
13
Medium Priority
?
3,129 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…

704 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