views:

646

answers:

5

Hi,

I have code to create another "row" (div with inputs) on a button click. I am creating new input elements and everything works fine, however, I can't find a way to access these new elements.

Example: I have element <input type='text' id='name_1' name="name_1" />
I then create element <input type='text' id='name_2' name="name_2" /> by using the javascript's createElement function.

Again, I create the element fine, but I want to be able to access the value of name_2 after it has been created and modified by the user. Example: document.getElementById('name_2');

This doesn't work. How do I make the DOM recognize the new element? Is it possible?

My code sample (utilizing jQuery):

function addName(){
var parentDiv = document.createElement("div");
$(parentDiv).attr( "id", "lp_" + id );

var col1 = document.createElement("div");
var input1 = $( 'input[name="lp_name_1"]').clone(true);
$(input1).attr( "name", "lp_name_" + id );
$(col1).attr( "class", "span-4" );
$(col1).append( input1 );

$(parentDiv).append( col1 );

$('#main_div').append(parentDiv);
}

I have used both jQuery and JavaScript selectors. Example: $('#lp_2').html() returns null. So does document.getElementById('lp_2');

+2  A: 

You have to create the element AND add it to the DOM using functions such as appendChild. See here for details.

My guess is that you called createElement() but never added it to your DOM hierarchy.

17 of 26
I am using jQuery's append( content ) function for adding my new elements. According to the documentation it is the same as appendChild(). Still no luck.
brostbeef
A: 

If it's properly added to the dom tree you will be able to query it with document.getElementById. However browser bugs may cause troubles, so use a JavaScript toolkit like jQuery that works around browser bugs.

Armin Ronacher
I am actually using jQuery, but I didn't think I needed to add that to the question. jQuery has been a big help though.
brostbeef
Can you obtain the element by using jQuery selectors, i.e. $("#name_2") instead of getElementById()? If not, that would at least help eliminate browser issues.
Adam Bellaire
I have tried both to no avail. Example: $('#lp_2').html(). (null)
brostbeef
A: 

var input1 = $( 'input[name="lp_name_1"]').clone(true);

The code you have posted does not indicate any element with that name attribute. Immediately before this part, you create an element with an id attribute that is similar, but you would use $("#lp_1") to select that, and even that will fail to work until you insert it into the document, which you do not do until afterwards.

Jim
The input element with a name of lp_name_1 is part of the page in its original form. I'm cloning that element to allow for a second name to be entered. The first name field will always be there. Another thing, creating the elements works, getting to them after doesn't.
brostbeef
A: 
var input1 = $( 'input[name="lp_name_1"]').clone(true);

should be

var input1 = $( 'input[@name="lp_name_1"]').clone(true);

Try that first, check that input1 actually returns something (maybe a debug statement of a sort), to make sure that's not the problem.

Edit: just been told that this is only true for older versions of JQuery, so please disregard my advice.

Athena
That's xPath syntax, it's unnecessary and hasn't worked in jQuery for a long time.
Jim
Oh cool, didn't realize that. Was playing around with a demo earlier, and the query wouldn't work without the @. Just double-checked and the site was using an older version of jquery, which explains it. Thanks for the info!
Athena
A: 

Hi all,

Thank you so much for your answers. After walking away and coming back to my code, I noticed that I had made a mistake. I had two functions which added the line in different ways. I was "100% sure" that I was calling the right one (the code example I posted), but alas, I was not.

For those also experiencing problems, I would say all the answers I received are a great start and I had used them for debugging, they will ensure the correctness of your code.

My code example was 100% correct for what I was needing, I just needed to call it. (Duh!)

Thanks again for all your help,

-Jamie

brostbeef