convert htmlelement to string for comparison javascript
Posted
by Jamex
on Stack Overflow
See other posts from Stack Overflow
or by Jamex
Published on 2010-06-14T20:31:48Z
Indexed on
2010/06/14
20:52 UTC
Read the original article
Hit count: 267
JavaScript
Hi,
I am using a function that obtains a target element id at onclick. Example, if I click on the text element that has the id of 'help'.
var click = (e && e.target) || (event && event.srcElement);
The var click would contain the ref to the id of "help".
I want to compare the var click to the string 'help' using the if statement below.
if (click == 'about') {do something}
The comparison does not work because the var click is not a string. When I use the alert(click) to debug, it shows click as "object HTMLElement".
How would you compare whether the id 'help' is obtained from var click?
I could write out something like
if (click == document.getElementById('help')) {do something}
but that would make a long statement.
also
if the var click is document.getElementById('help'), how would you make a new var "show" as document.getElementById('showhelp')
basically, I want to use the same function to generate dynamic responses to each element that was clicked on, and not having to create a separate function for each element.
© Stack Overflow or respective owner