1

I have tried rectifying the code below. But I am not able to find a solution. After executing the code, firebug says "document.getElementById(haystack.value) is null". I tried if(document.getElementById(haystack).value ==null) but it was of no use. Please help me out.

     var haystack=document.getElementById('city1').value;
 if(!document.getElementById(haystack).value)
 {
   alert("null");
 }
 else
 {
   alert("not null");
 }

Edit:

haystack gets the value of the city. When i try an "alert" on haystack -alert(haystack) i get a positive reply. but when i try it with "document.getElementById(haystack).value" i get an error. One thing though, the element with the id that haystack gets might or might not exist.

Edit again:

I think ill kill myself. I put the city for the name attribute for an input element not the id attribute. I am sorry, but sitting in front of the computer this long made me loose my mind. But it is no excuse for having wasted your time. Please accept my sincere apologies. Thanks spender for helping me out.

8
  • 1
    Don't explain how you think you can solve your problem, explain your problem. What is haystack? What are you trying to achieve? Commented Apr 22, 2010 at 8:47
  • haystack gets the value of the city. i am tyring an alert on haystack alert(haystack) and i get a positive reply. but when i try it with document.getElementById(haystack).value i get an error. Commented Apr 22, 2010 at 9:22
  • terribly sorry for being incomplete Commented Apr 22, 2010 at 9:25
  • So there's an element with id "city1" that has a value that corresponds to the id of another element in your DOM? Is this what you are trying to do? Commented Apr 22, 2010 at 9:30
  • 1
    So, in the case that haystack=="florida", if you also include the line alert(document.getElementById("florida")==document.getElementById(haystack)) what do you see? Commented Apr 22, 2010 at 9:57

4 Answers 4

7

You're trying to look up a property on document.getElementById('city1') which might be null. Try this instead:

var haystackElement=document.getElementById('city1');
if(!haystackElement)
{
    alert("haystackElement is null");
}
else
{
    alert("haystackElement is not null");
    var haystack=haystackElement.value;
    if(!haystack)
    {
        alert("haystack is null");
    }
    else
    {
        alert("haystack is not null");
    }

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

3 Comments

Sure, but document.getElementById('city1').value will error immediately if no such element is in the DOM.
Yes - you'll get an exception because you dereferenced a null. Checking for whether the element exists first is best.
Downvote with no comment. To the downvoters: SO encourages downvoters to leave a comment.
0

You already have your haystack object:

var haystack=document.getElementById('city1');
if(!haystack.value)
{
  alert("null");
}
else
{
  alert("not null");
}

document.getElementById is used to get the element, you have done that and placed it in the haystack variable. There is no need to call document.getElementById again on it (and incorrectly at that). Read about getElementById.

1 Comment

This will still report that document.getElementById('city1') is null - you need to check for this before inspecting its value.
0

Unfortunately you have shown us some code and described an error (which looks like it has been transcribed incorrectly) without telling us what you are actually trying to achieve.

Take a look at this code sample, it fixes the robustness issues with your code, and has more detailed alert messages to make it clear what is being detected.

Hopefully it will clear things up for you.

var haystack = document.getElementById('city1').value;
var haystack_element = document.getElementById(haystack);
if (haystack_element) {
    if (haystack_element.value) {
        alert("The element has a true value");
    } else {
        alert("The element has a false value, such as '' or 0");
} else {
    alert("No element with that name");
}

Comments

0

Doing it in less lines:

if ((el = document.getElementById('city1')) && el.value) {
    alert ("not null");
} else {
    alert ("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.