Solved

How to replace img onclick event using javascript

Posted on 2008-10-16
13
3,076 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult 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…

867 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now