0

I have two functions:

function changeImage(imgName)
{
image = document.getElementById('imgDisp');
image.src = imgName;
}

function changeSize()
{
document.getElementById('imgDisp').setAttribute("datamask","../files/test/2.png");
}

They are both called with an "onclick" event:

 <img id="imgTemp" onclick="changeImage('../files/templates/blank/<? echo($one['id'])?>.jpg');changeSize();"

What I want is so that changeSize is called again after changeImage is executed. I was advised to use callback but most examples I have seen deal with variable callback inside a function and not with two functions.

Edit: The function change size should put a mask on the image, giving the "datamask" as a parameter to another function in another .js file. The image gets the right "datamask" parameter, but the mask is not applied.

6
  • 5
    Simply call changeSize() in changeImage function Commented Apr 8, 2014 at 8:03
  • 1
    It is actualy called right after changeImage is executed. I don't get what's wrong with this .. :/ Commented Apr 8, 2014 at 8:13
  • any error in your browser console Commented Apr 8, 2014 at 8:14
  • should work fine - jsfiddle.net/arunpjohny/J424R/1 Commented Apr 8, 2014 at 8:16
  • The above code should work, assuming that you actually close the img tag and you change its id to imgDisp. Provide more information about the behaviour you expect and you get. Commented Apr 8, 2014 at 8:18

4 Answers 4

1

You can call changeSize() inside changeImage() instead:

function changeImage(imgName)
{
image = document.getElementById('imgDisp');
image.src = imgName;
changeSize();
}

then you can remove changeSize(); from your onclick function:

<img id="imgTemp" onclick="changeImage('../files/templates/blank/<? echo($one['id'])?>.jpg');" />
Sign up to request clarification or add additional context in comments.

Comments

0
function changeImage(imgName)
{
image = document.getElementById('imgDisp');
image.src = imgName;
changeSize();
}

function changeSize()
{
document.getElementById('imgDisp').setAttribute("datamask","../files/test/2.png");
} 


<img id="imgTemp" onclick="changeImage('../files/templates/blank/<? echo($one['id'])?>.jpg');changeSize();"/>

Comments

0

Just write changeSize() to call the function.

Comments

0

This is an example, but I would place the url you echo out in the html attribute in a data attribute. This would be a bit more stable and clean. http://jsbin.com/gijiketi/1/edit?html,js

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.