Jump to content

problem with onclick SOLVED with our thanks in the final post.


niche

Recommended Posts

i would just set an id on the <img> element instead, i think value only applies to input elements. also, the logic for setting the display style values seemed a little off.http://jsfiddle.net/nJU4F/

Link to comment
Share on other sites

Thanks, but I need to target the id of the hidden div not the visible div. I need the onclick in the visible div to toggle the display property from off to on. I don't understand why the showDiv() function isn't listening.

Edited by niche
Link to comment
Share on other sites

Turns out there's no value attribute for a div or img. I had to use data-value to get the target value in my function. In case you're interested, here's my solution (still needs a little positioning):http://www.savebop.com/test_120810c.php Thanks for your help thescientist.

Link to comment
Share on other sites

The new property to access data attribute is dataset, but as usual IE is still slow on the uptake so we still have to use getAttribute as backup.

function showDiv(elem) {var id_ref = elem.getAttribute("data-idref") || elem.dataset.idref;var divTest = document.getElementById(id_ref);if (divTest.style.display === "none") {   	 divTest.style.display = '';    } else if (divTest.style.display === '') {	      divTest.style.display = "none";        }               }

Link to comment
Share on other sites

ALL browsers will be able to use getAttribute, the new method is to use dataset but its not supported in IE, yeah! just relised its always going to use getAttribute try this instead

function showDiv(elem) {var id_ref;if(elem.dataset){id_ref =  elem.dataset.idref;}else{id_ref = elem.getAttribute("data-idref");}var divTest = document.getElementById(id_ref);if (divTest.style.display === "none") {        divTest.style.display = '';    } else if (divTest.style.display === '') {	      divTest.style.display = "none";         }              }

Link to comment
Share on other sites

Why did you change the argument in the showDiv() function ( http://www.savebop.com/test_120810c.php ) from:

function showDiv(value) {

to

function showDiv(elem) { 

???

Edited by niche
Link to comment
Share on other sites

Because it refers to the element on which onclick was triggered, not its value, just my way of making it more understandable an readable on what it is referring to. AS in with if(elem.dataset){id_ref = elem.dataset.idref;}else{id_ref = elem.getAttribute("data-idref");} you are looking at that specific elements attributes

Edited by dsonesuk
Link to comment
Share on other sites

I'm confused about elem as an argument. This is the element that triggered showDiv() in my #7 post in http://www.savebop.com/test_120810c.php.

echo '<button style="float:left;" class="button4"  onclick="showDiv(this.value)" value="relatedItems' . $counterz . '">Related Items</button>';

What do I need to do to redefine the showDiv() argument, in the element, to get the showDiv() function to start listening? The console says elem is undefined.

Edited by niche
Link to comment
Share on other sites

Dsonesuk: Based on http://www.savebop.c...est_120810c.php in post #7 was it the relatedItems(event) function you meant to modify instead of showDiv(value)? It's the relatedItems(event) that makes use of data-value.

Edited by niche
Link to comment
Share on other sites

I was going by your original example giving you a example on how to use dataset

function showDiv(elem) { var id_ref; if(elem.dataset){id_ref =  elem.dataset.idref;}else{id_ref = elem.getAttribute("data-idref");} var divTest = document.getElementById(id_ref); if (divTest.style.display === "none") {   	 divTest.style.display = '';	} else if (divTest.style.display === '') {		  divTest.style.display = "none";		}			  }

  <img onclick="showDiv(this)" data-idref="relatedItems2"" style="width:5%;" src="http://www.savebop.com/stk/lams/BATBOAT.JPG" alt="BATBOAT" /> <div id="relatedItems2" style="display:none;width:100%"> <img  src="http://www.savebop.com/stk/lams/BATBOAT.JPG" alt="BATBOAT" /> </div>

Edited by dsonesuk
  • 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
×
×
  • Create New...