Jump to content
DNA180

Javascript function works on second click and on!

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

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

My friend "Deirdre's Dad" thank you very much for your help!!! You are right... I just removed the second part of IF and it works fine!!! :DMy friend "thescientist" thank you too for your answer!!!

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...