0

I have this image in my html page:

<img src="/images/deactivate.png" onclick="act_deact(this);"/>

and in my javascript code i have this:

function act_deact(image) {
    image.src = (image.src=="/images/activate.png" ) ? "/images/deactivate.png" : "/images/activate.png";
}

and when i click on the image that initially deactivated it activate but in the second click it doesn't desactivate !

is there any probleme with my code ?

from JS amateur :)

2
  • Well it works for absolute URLs: jsfiddle.net/4rNha/1 Commented Apr 25, 2013 at 11:06
  • @Antony : yes i have tried that with the server adress and it works. but i work in localhost and i dont want to make the complete url with host and port for just making it works !, is there an other solution to make it work with relatif url ? Commented Apr 25, 2013 at 11:14

6 Answers 6

4

This is because when you set it to(or it changes to) /images/activate.png on first click, the src attribute is no longer just /images/activate.png but it gets prefixed with the server-address.

You can check it here: http://jsfiddle.net/hGcqq/

Or on your own server, use a console.log or an alert if you prefer.

Sign up to request clarification or add additional context in comments.

Comments

0

@hjpotter92 already explained the reason it doesn't work.

One way to make it work is to check if src ends with /images/activate.png:

var activateURL = '/images/activate.png';
if (img.src.substring(img.src.length - activateURL.length) !== activateURL) {
    image.src = '/images/activate.png';
} else {
    image.src = '/images/deactivate.png';
}

1 Comment

yes good idea, i will try to find activate or deactivate in the url and make condition of returned result :)
0

The image.src contains the canonical path, even if initialised with a relative path. You can either use the full path in the condition like

function act_deact(image) {
    image.src = (image.src=="FULL-PATH-OF-IMAGE" ) ? "/images/deactivate.png" : "images/activate.png";
}

(to get the full path one may use alert(image.src);)

or, if all your images have unique names

function act_deact(image) {
    image.src = image.src.match(/activate.png$/) ? "/images/deactivate.png" : "images/activate.png";
}

Comments

0

The first thing is brackets which should be like this:

image.src = (image.src=="/images/activate.png" ? "/images/deactivate.png" : "/images/activate.png")

Also try image.getAttribute("src") and image.setAttribute("src") instead of image.src.

Please try this code:

function act_deact(image) {
    image.setAttribute("src", (image.getAttribute("src")=="/images/activate.png" ? "/images/deactivate.png" : "/images/activate.png"))
}

Edit: other answers tell you about the issue of absolute and relative urls, so getAttribute will fix that issue. I do not recommend to use RegExp or substrings here.

Comments

0

To make it work with relative url you can simply save current url like this:

var newsrc = "/images/deactivate.png";
function act_deact(image) {
  if ( newsrc == "/images/deactivate.png" ) {
    image.src = "/images/activate.png";
    newsrc  = "/images/activate.png";
  }
  else {
    image.src = "/images/deactivate.png";
    newsrc  = "/images/deactivate.png";
  }
}

and then call it

<img src="/images/deactivate.png" onclick="act_deact(this);"/>

3 Comments

Why not just check based on img.alt attribute?
That would probably work too but the original question didn't have it so I removed that part of code from my answer. =)
Using alt imho, was better though.
0

I found the solution myself :p and i have tried @fardjad proposition that is looking for string in the url and make conditions

i share the solution :)

function act_deact(image) {
if(image.src.indexOf('deactivate') != -1)
   image.src="/images/activate.png";
else 
   image.src="/images/deactivate.png";
}

@hjpotter92: the solution with check condition on image.alt works to:

html:

<img alt="deactivate" src="/images/deactivate.png" onClick="act_deact(this);" />

the Js function :

function act_deact(image) { 
    if (image.alt=="activate") {
       image.src = "/images/deactivate.png";
       image.alt = "deactivate"                                          }
    else {  
       image.src = "/images/activate.png"; 
       image.alt = "activate"
    }
}

thanks for your responses :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.