/    Sign up×
Community /Pin to ProfileBookmark

getElementById doesn’t work with Combobox

Hi
my code is above:
function createCombo(){
var selectElement = document.createElement(“SELECT”);
selectElement.setAttribute(“ID”,”1″);
selectElement.setAttribute(“name”,”phoneModels”);
alert(document.getElementsByName(“phoneModels”));
for(rowIndex = 0; rowIndex < models.length; rowIndex++ ){
var oOption = document.createElement(“OPTION”);
selectElement.options.add(oOption);
oOption.innerText = models[rowIndex][0];
oOption.value = models[rowIndex][1];
}
alert(document.getElementById(“1”));
}
alert(document.getElementsByName(“phoneModels”)) command prompts [B]object[/B]

alert(document.getElementById(“1”)) command prompts [B]null[/B]

i couldn’t solve the problem
please help
thanks

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@yellabuffApr 25.2006 — Try changing uppercase ID to lowercase id and appending the select element to the form.

[COLOR=DarkRed]<form name="formOne"></form>[/COLOR]

<script>

[COLOR=DarkRed]frm = document.formOne;[/COLOR]

function createCombo(){

var selectElement = document.createElement("SELECT");

selectElement.setAttribute("[COLOR=DarkRed]id[/COLOR]","1");

selectElement.setAttribute("name","phoneModels");

for(rowIndex = 0; rowIndex < 2; rowIndex++ ){

var oOption = document.createElement("OPTION");

selectElement.options.add(oOption);

//oOption.innerText = models[rowIndex][0];

//oOption.value = models[rowIndex][1];

}

[COLOR=DarkRed]frm.appendChild(selectElement);[/COLOR]

}

[COLOR=DarkRed]createCombo();[/COLOR]

alert(document.getElementById("1"));// command prompts [object]

</script>
×

Success!

Help @pornova spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...