tags:

views:

522

answers:

4

Hello:

I am trying to write a javascript function with an "if" statement that will execute a command if the text between two tags match. I have been able to write many "if" statements in which a command is executed when the value within an input textbox equals that of the "if" statement criteria such as:

function Test()
{
    if (document.getElementById('A').value == 1)
    {
      alert('test');
    }
}

<input type="button" id="B" onCLick="Test()"/>
<input type="text" id="A"/>

When I enter "1" in the textbox and press the button, an alert box will appeare. The problem that I am having is that I would like to do the same with text bewteen two tags. For example:

function Test()
{
        if (document.getElementById('A').value == 1)
    {
      alert('test');
    }
}

<input type="button" id="B" onCLick="Test()"/>
<p id="A">1</p>

In my project I would be using words instead of numbers, so I understand that I would have to surround the word in quotes. Unfortunately, this doesn't work. Is it possible to write an "if" statement like the one above that determines if text between two tags is true? Also, I have tried document.getElementById('A').text, and document.getElementById('A').innerHTML, but none of those made a difference. I ave even tried using one equal sign instead of two; however, that would make all criteria true, regardless if it were true or not.

Thanks

DFM

A: 

This should do what you want.

<html>

  <input id="test" type="text" value="hello">

  <script>
    if (document.getElementById('test').value == "hello") alert("hello");
  </script>

</html>
Andrew Johnson
Thanks Andrew - I did not know that I have to declare a value within the tag to be able to refer to it with my "if" statement. I just tried this method in my project and it works well.Thanks,DFM
using value to get the text inside a <p> element will not work
Russ Cam
If you want the value in a <p> tag, you need to write: if (document.getElementById('test').innerHTML == "hello") alert("hello");
Andrew Johnson
+1  A: 

You have onCLick instead of onclick (all lowercase) in your second example, which is why it is not working. You should use innerHTML to get the text inside the <p> element.

Note however, that were you to have other elements inside of an element that you wanted to get the text for, using innerHTML will return all of those elements and their respective content too.

Working Demo

Russ Cam
Thanks Russ - For some reason I was not running into any problems with onClick. Things like backgroundColor, where there has to be a capital have been throwing me off. Also, I tried innHTML, but my syntax might have been wrong. I am using Notepad, so it's trial and error here.Thanks,DFM
at the very least then, go download Notepad++ - http://notepad-plus.sourceforge.net/uk/site.htm or Aptana Studio is very sweet too - http://aptana.com/studio/ both are free and superior in features to notepad for writing code in.
Russ Cam
onclick is part of the HTML specification - http://www.w3.org/TR/REC-html40/interact/scripts.html JavaScript keywords are in camelCase
Russ Cam
+3  A: 
if(document.getElementById("myID").innerHTML == "1")

Would evaluate to true if the element in question (e.g a div) had the following markup.

<div>1</div>

You could also use jQuery

if($(element).text() == "1")
Tyler
A string and an integer would evaluate to true with == because it ignores the type. It would only evaluate to false if you used === since that checks for value as well as type. But a good practice to get into nonetheless.
Perspx
He is using == which does not type-check in JS, so "1" == 1. 1 !== "1" though.
Thorbjørn Hermansen
Edited. Thanks for the heads up
Tyler
A: 
sfjedi