niche Posted May 22, 2012 Share Posted May 22, 2012 (edited) 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 May 22, 2012 by niche 1 Link to comment Share on other sites More sharing options...
eTianbun Posted May 22, 2012 Share Posted May 22, 2012 I dont get what you mean, explain in a clear terms. Link to comment Share on other sites More sharing options...
niche Posted May 22, 2012 Author Share Posted May 22, 2012 (edited) 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 May 22, 2012 by niche Link to comment Share on other sites More sharing options...
Ingolme Posted May 22, 2012 Share Posted May 22, 2012 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. 1 Link to comment Share on other sites More sharing options...
niche Posted May 22, 2012 Author Share Posted May 22, 2012 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now