Jump to content

Javascript function works on second click and on!


DNA180
 Share

Recommended Posts

Hello guys!!! :DI think I need your help here... I have made this JS code below in to my site and I have a problem. When I click on maximize icon, first time doesn't work and it works on second click!!! Does anybody knows what am I doing wrong???FIDDLE EXAMPLE: HERE

CODE EXAMPLE:

function Images_MaxMin(){		var div_1   = document.getElementById("image_max");var div_2   = document.getElementById("image_min");var big_img = document.getElementById("big_image");var img_url = document.getElementById("img_url");                          if (div_1.style.display == 'block' || div_1.style.display == '')     {div_1.style.display = 'none';      big_img.src         = '';      div_2.style.display = 'block';}else {div_1.style.display = 'block';      big_img.src	  =  img_url.value;       div_2.style.display = 'none';}}

Thank you in advance!!!

Edited by DNA180
Link to comment
Share on other sites

one thing I see, is your else conditional is incorrect, you are using assignment instead of testing for equality, like you are doing in the if clause.

 

If you are trying to figure out why your code isn't working, you should learn to debug and check for errors. Open your error console, or add console logging to make sure the elements and variables you are testing/checking against are what you expect them to be, and to log the flow of your code.

Link to comment
Share on other sites

The problem is your conditional:

 

if (div_1.style.display == 'block' || div_1.style.display == '')

When the page loads, div_1 displays none, so the first test returns false. But style.display IS an empty string, so the second test returns true.

 

Change your conditional.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...