Solved

How to replace img onclick event using javascript

Posted on 2008-10-16
13
3,099 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
REST call Failing 1 41
Geolocation works...sometimes 2 40
jQuery element reference issue 2 41
Leaflet map example works but I want to make a change to it 5 30
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
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…

710 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