1

I am using prototype.js

After dynamically generated Input elements when I do

document.getElementById() or $(Id)

I get null. UPDATE:

enter image description here

3
  • at the time of calling document.getElementById or $(Id), does the component exist in the source of page? Commented Dec 9, 2011 at 13:25
  • create a jsfiddle to explain your question.. nobody can not help with this chunk of code. Commented Dec 9, 2011 at 13:25
  • Your element is already appended to DOM, or you just archiving it in a var? If you appended it to DOM, did you wait persist the element creation before search in the document for the ID? Commented Dec 9, 2011 at 13:26

4 Answers 4

2

It seems to me like you did not give any id to your input field. it looks like you only have name attribute. you should give it an id attribute too.

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

1 Comment

exactly the id is missing from the dom elements
1

Querying the dom for elements that are dynamicly generated doesn't make a lot of sense.

When you generated the element, save a reference to it. That way your javascript doesn't do a lot of repetitive expensive queries.

To answer you specific question, it should work, and you probably forgot to insert the element into the dom.

4 Comments

how would i insert it in dom?
@Ravi ~ oh no. there's your problem. :)
the id is missing from the elements, as you can see from firebug they are part of the DOM
The image wasn't part of the question when I answered, so I could only guess.
1

The element selected with Firebug does not have an ID attribute. Therefore, you won't be able to get it by id.

Comments

0

As per my knowledge you are suppose to writing your JavaScript at the top of the page but the element will generate after some execution or after the execution of that java script code.

So in that case particular JavaScript code didn't got that element ID; so it will retrieve a null value and send error like that.

If possible just try to call that JavaScript at the bottom of the page OR we can say after page get load.. i think this will work for your same code..

Just try it once, if your luck with you then will get your Element Id.

Thanks.

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.