javascript - alert when a var equal div value -
i trying to show alert message when javascript var equal div value , here trying here :
function checkmydiv() { var elementcssclass = document.getelementbyid("target"); if (elementcssclass == "hello") { alert("the div has value"); } } here html code
<div id="target" >hello</div> <input type="button" value="bader" onclick = "checkmydiv();" /> i not know did go wrong , nothing popup when click button bader
appreciated :)
you need text content element. right now, you're comparing object reference against hellowhich won't work.
if (elementcssclass.textcontent == "hello") { } is you're looking for.
one word auf caution: .textcontent supported modern browsers (=latest versions). however, old'ish version of ie might not recognize property. might want either like
var mytext = elementcssclass.textcontent || elementcssclass.text; and compare value mytext against hello or use elementcssclass.innerhtml. i'd prefer former solution because using .innerhtml might bring in trouble in sitations.
Comments
Post a Comment