Reputation: 117
I have a view in an MVC web application that produces and presents a list of countries and the name of their capital cities. Whenever the view loads, if the results presented to the user contain the words 'Australia' one or more times, then I want a message to appear at the bottom of the screen that says 'Australia is included in the results'. I have used the following script, but the message appears whether or not Australia is in the results. Does anyone have any suggestions?
<script type='text/javascript'>
if (
(
document.documentElement.textContent || document.documentElement.innerText
).indexOf('Australia') > -1
) {
alert("Australia is included in the results");
}
</script>
Thanks in advance
Upvotes: 0
Views: 1254
Reputation: 278
This is actually an answer for your follow up question about using HTML instead of an alert (I can't post comments, so had to do it this way, sorry.)
What you want is document.getElementById("element_ID").innerHTML = "whatever";
See here: http://jsfiddle.net/UVU8w/
Upvotes: 0
Reputation: 11810
Your code looks sensible, so I suspect that the problem is that it is finding the string "Australia" in the code its self!
Try putting this script outside of the page contents (in a separate .js file) and see if the problem persists.
Maybe ;-)
Upvotes: 1
Reputation: 2774
I think only this condition might suffice:
if (document.documentElement.innerText.indexOf('Australia') > -1) {
alert("Australia is included in the results");
}
Upvotes: 1