0

post submit I want to javascript/client side read user input

var h = document.getElementById("myTEXTbox").value;

but the textbox only exists depending earlier user choices. is the an if object exists in javascript?

1
  • Can you rephrase your question? What are you trying to do? Commented Apr 1, 2014 at 16:22

3 Answers 3

1

check the return of getEelementById, if an element doesnt exist it will return null, and since it is a falsey value you can use it in a if statement.

var element = document.getElementById("myTEXTbox");
var h =  element ? element.value : "";

So if element is not null, h will be set to element.value otherwise it will be set to an empty string

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

Comments

1

If the object exists then it is an object, if not, it will return undefined!

So you can test it like this:

var element = document.getElementById("myTEXTbox"),
    h = (element)? element.value : '';

Basically, you are using this logic: If the element does exist "(element)" or alternatively (el!=undefined) then read the value, if not set the value of 'h' to '' (empty). This could be null or another value.

Comments

0

Try this:

var element = document.getElementById("myTEXTbox"),
    h = element ? element.value : null;

Comments

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.