0

I'm a beginner trying to get the HTML from a textbox to be used in an if/else statement. This is my HTML code:

    <label id="label1">
    Enter any Number:
</label>
<input type="button" id="Button1"  value="button" />
    <input type="text" id="TextBox1" name="myname" />

And my JavaScript code is:

    <script type="text/javascript">
//<![CDATA[
var buttonElement = document.getElementById("Button1");
var txt_value =document.getElementById("TextBox1").value;
buttonElement.addEventListener('click', function() { Clicked(txt_value) }, false);

function Clicked(txt_value) {

    if (txt_value == 7) {
        alert("You are 7");
    }
    else { alert("You are not 7"); }
}
//]]>
    </script>

I observed that

    var txt_value =document.getElementById("TextBox1");

and then

      buttonElement.addEventListener('click', function() { Clicked(txt_value.value) }, false);

The above example works absolutely fine.

Can someone please tell me what is wrong with:

    var txt_value =document.getElementById("TextBox1").value;

I don't know why I'm getting an empty txt_value

2 Answers 2

1

The reason is that you are getting the value in txt_value before the user enters anything; hence the value is always empty.

IF you change your code to this:

var txt_value =document.getElementById("TextBox1");//removed .value

And the function Clicked to:

function Clicked(txt_value) {

    if (txt_value.value == 7) { //added .value
        alert("You are 7");
    }
    else { alert("You are not 7"); }
}

Should work.

Here's a jsfiddle

Sign up to request clarification or add additional context in comments.

7 Comments

thnks :) ya tht wrks i know. So Reason is m trying to access the value before user enters so getting empty nice thanku
@ShilpaGurnani I am glad I was able to help. I notice that you are knew here... please don't forget to accept the answer that helped you the most. This will increase the chances of getting help here in the future.
is that because of using addEventListener otherwise if m must have written onclick within html tag it must have wrked fine with var txt_value =document.getElementById("TextBox1").value;
ya sure :) i was accepting the answer but its like i got prompt that u can accept after 6 minutes
@ShilpaGurnani the problem is that your variable assignment (var txt_value=document.getElementById('TextBox1').value) occurs immediately when the page loads, before the user has a chance to interact with the page; hence the txt_value variable ends up being blank. You need to read the value only when the user clicks the button. It's okay to get a reference to the text input element when the page loads, but you need to read the value only when the user clicks.
|
1

Move the getting of the value into the click handler...

var textbox1 = document.getElementById("TextBox1");

document.getElementById("Button1").onclick = function () {
    var txt_value = textbox1.value;

    if (parseInt(txt_value, 10) === 7) {
        alert("You are 7");
    } else { 
        alert("You are not 7"); 
    }
};

Now you get the value that is in the textbox when the page loads.

Here is a JSFiddle to test this.

Update Improved the efficiency by caching the textbox. Removed the addEventListener to an onclick (more browser support)

1 Comment

Actually i read some where that latest is like use event listners instead of onclick hence used and btw i my addEventListener works for chrome and addEvent works for IE So Is ny other way which can be used across all browsers except the above onclick in tag?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.