i am trying to to show an alert message when my javascript var equal div value , here is what i am trying here :
function checkmydiv() {
var ElementCssClass = document.getElementById("Target");
if (ElementCssClass == "hello")
{
alert("the div has a value");
}
}
here is my html code
<div id="Target" >hello</div>
<input type="button" value="bader" onclick = "checkmydiv();" />
i do not know where did i go wrong , nothing popup when i click the button bader
appreciated all the help 🙂
You need to get the text content from that element. Right now, you’re comparing the object reference against
hellowhich obviously won’t work.is what you’re looking for.
One word auf caution:
.textContentis supported by all modern browsers (=latest versions). However, old’ish version of IE might not recognize that property. So you might want to either do something likeand then compare the value from
myTextagainsthelloor just useElementCssClass.innerHTML. I’d prefer the former solution because using.innerHTMLmight bring you in trouble in another sitations.