Jump to content

id v variable SOLVED with thanks in the last post


niche
 Share

Recommended Posts

This just soaked in. I suppose it can be called targeting by variable. What are the pros/cons of targeting by id vs variable where targeting by variable is defined as:

<head><script type="text/javascript">function display(el) {   if (el.innerHTML == 'Send BOP to Cart') {	  el.innerHTML = 'Sent BOP to Cart';   } else {	 el.innerHTML = 'Send BOP to Cart';   }   //var test = el.innerHTML;   //alert(test);}</script><style type="text/css">#button2 {padding:0px;width:82px;height:40px;vertical-align:top;}</style></head><body><button id="button2"	onclick="display(this)">Send BOP to Cart</button></body></html>

Edited by niche
  • Like 1
Link to comment
Share on other sites

In this case, el.innerHTML = does the same thing as document.getElementById('button2').innerHTML =. Is that a fact or does it just seem that way? EDIT: What are the best practices issues if any?

Edited by niche
Link to comment
Share on other sites

They're both references to an element, that's all.getElementById() is a function that returns a reference, while a variable already has the reference in it. It doesn't matter, in the end, how you referenced the element.

  • Like 1
Link to comment
Share on other sites

That's good news. I'm just trying to keep the self-inflicted wounds down to a dull roar. Thanks Ingolme and CodeName.

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...