Reputation: 1969
<p id="test">lol</p>
<script type="text/javascript">
var text = $('test').text();
var comparingText = 'lol';
if (text == comparingText) {
document.write('haha');
}
</script>
Why will not this work ?
Have even tried using "IF NOT
"..
Upvotes: -1
Views: 82
Reputation: 26930
You are not getting element by id, should be:
var text = $('#test').text();
And it would be better to write your script in DOM load event:
$(function(){
var text = $('#test').text();
var comparingText = 'lol';
if (text == comparingText) {
document.write('haha');
}
);
Fiddle: http://jsfiddle.net/eedUs/1/
Upvotes: 0
Reputation: 324620
You are attempting to grab a <test>
tag, not a tag with ID test
. To get the tag by ID, you need a #
symbol: $('#test')
HOWEVER: Using jQuery as a selection engine is overkill and inefficient. Here is your code in vanilla JavaScript:
if( document.getElementById('test').firstChild.nodeValue == "lol")
document.write("haha");
Upvotes: 4