0

My DOM skills are weak and I can't work out why a javascript variable is assigned this object pointer value rather than the string value itself.

Short story is that I am making an AJAX call for some screen data and it returns html and populates a div.innerHTML with the following :

 <input id="page_tag_add_input"></input>';

    <span class="page_tag_add"><a href="#" onclick="var newTag = document.getElementById('page_tag_add_input').value; doTagXhr('add_tag.php', newTag); alert(newTag);">Add</a></span>

The doTagXhr function is a YUI connection manager AJAX call.

When the user clicks add, firebug shows me that the newTag variable is stored as "[object HTMLDivElement]", yet when the alert(newTag) javascript kicks in (above), it shows the value correctly as the input text string?? I've exhausted Google searches :-(

Are there any gurus out there that can point me in the right direction? Thanks.

1 Answer 1

1

You're assigning newTag to a DOM Element property. I think you've mistaken what Firebug reports it as, that code indicates it's clearly not an element reference, and unless you're manipulating it in the xhr function ( which you didn't paste the code to ) then it's still a string.

Edit:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script>
    function doTagXhr(page, input) {
    window.input = input;
    $.ajax({
        url:page,
        type:'POST',
        data:input
    });
    }
</script>
<input id="page_tag_add_input" value="test">

    <span class="page_tag_add"><a href="#" onclick="var newTag = document.getElementById('page_tag_add_input').value; doTagXhr('regex.php', newTag); alert(newTag);">Add</a></span>

This always stays a string. I don't know what else it could be other than your xhr function that's reassigning the variable.

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

4 Comments

Thanks for the prompt answer. Sorry, maybe I worded that wrong. When I refer to Firebug, I mean to say that I can see the newTag param that is being POSTed and it is sending the string "[object HTMLDivElement]" rather than the value of the input field that I want.
But yes, your comment about assigning a variable to the DOM Element property sounds interesting. Even if I don't declare a variable and put the getElementById directly into the function call, i.e.: doTagXhr('add_tag.php', document.getElementById('page_tag_add_input').value) I get the same issue... hmm? More pondering needed. Also more coffee.
can you post the code to your xhr function? i replicated what you are trying to do and I'm not experiencing what you are. It has to be that XHR function.
Lifesaver! User error on my part. My doTagXhr function had an overlap with tag name variables and it was picking up the contents of another html item. Thank you very much!!

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.