Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cannot access newly created item with getElementById

Status
Not open for further replies.

gbrian

IS-IT--Management
Sep 22, 2004
96
US
I have a function that creates 'select' controls:

var newInput4 = document.createElement("select");
newInput4.setAttribute("id","phonetype" + (howmanyrows+1));
newInput4.onchange = new Function("pt1(" + spanStart.id + ", " + newInput4.id + ")");



After this object is created (newTD2.appendChild(newInput4);) -- the function pt1() attempts to access the selection through:

var pt = document.getElementById(select);
phoneType = pt.value;

However, it tells me the ID (the value of select) is undefined.

Why can't I access the object I've created? I've also tried document.all.phonetype2...... and this does not work either.

PLEASE HELP!!
 
Have you tried var pt = document.getElementById("select");?

[blue]Go to work to learn. Don't go to work to earn.[/blue]
 
Try this.
>newInput4.onchange = new Function("pt1(" + spanStart.id + ", " + newInput4.id + ")");
[tt]newInput4.onchange = new Function([red]""[/red],"pt1(" + spanStart.id + ", " + newInput4.id + ")");[/tt]

 
Further note:
It seems those parameters are built as literal. The change I made is not thorough enough. I would have meant this.
[tt] newInput4.onchange = new Function([red]"",[/red]"pt1([red]'[/red]" + spanStart.id + "[red]'[/red], [red]'[/red]" + newInput4.id + "[red]'[/red])");[/tt]
 
The onchange portion of my select control passes the ID (in my case it happens to be 'phonetype2' to the pt1 function. The error comes up saying "phonetype2" is undefined.

The onchange / function seems to be working fine--the problem is when attempting to do this:
var pt = document.getElementById(select);

where select = "phonetype2"

It seems like this element's ID is either not being set properly or for some reason the function can not "see" the element on the page.

Any other ideas?
 
Perhaps you can verify the select element has an ID of "phonetype2". We cannot, as you have not shown us the HTML.

Also, check you do not have multiple elements with an ID of "phonetype2".

Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
I definitely do not have multiple elements with the ID of phonetype2.

When the item is added after the page has finished loading (IE--this element is loaded on the page by clicking a link), I cannot "view the html" to verify.

However, I did include the following line of code as part of creating the element:

newInput4.setAttribute("id","phonetype" + (howmanyrows+1));

This should set the ID, correct? How can I verify? I would think getElementById(..my id here..) not working would verify the ID is not set for some reason...

 
Dan, that is how I originally had it. I tried using setAttribute thinking .id wasn't working properly.

Still no luck..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top