/    Sign up×
Community /Pin to ProfileBookmark

getElementByName

I cannot figure out why this seemingly simple code won’t work, the “else if” line.

[code]<script type=”text/javascript”>
<!–
function checkKeywords(x)
{
if(x==1) { //
document.getElementById(‘Lights’).checked = true;
document.getElementById(‘Lighting’).checked = true;
document.getElementById(‘Light’).checked = true;
document.getElementById(‘Lamp’).checked = true;
document.getElementById(‘Table’).checked = true;
} else if(x==2) {
document.getElementsByName(‘keyword’).checked = false;
}
}

// –>
</script>

<label><input type=”radio” name=”Category” value=”table_lamp” onclick=”checkKeywords(1);”>Table Lamp</label><br>
<label><input type=”radio” name=”Category” value=”floor_lamp” onclick=”checkKeywords(2);”>Floor Lamp</label>
<br><br>
<label><input type=”checkbox” name=”keyword” value=”Lights” id=”Lights”>Lights</label><br>
<label><input type=”checkbox” name=”keyword” value=”Lighting” id=”Lighting”>Lighting</label><br>
<label><input type=”checkbox” name=”keyword” value=”Light” id=”Light”>Light</label>[/code]

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@A1ien51Sep 01.2009 — Because it returns an array and you would need to loop through each one.

Eric
Copy linkTweet thisAlerts:
@SwirlykingauthorSep 01.2009 — Hi Eric,

thank you so much. Sorry to be such a newbie, but could you show me how that would look?
Copy linkTweet thisAlerts:
@justinbarneskinSep 01.2009 — } else if(x==2) {alert(document.getElementsByName('keyword').length)
Copy linkTweet thisAlerts:
@SwirlykingauthorSep 01.2009 — Justin, thanks. It gets me an alert window that gives me "3", but how do I use that to uncheck all the checkboxes?
Copy linkTweet thisAlerts:
@justinbarneskinSep 01.2009 — Hmm, I don't know why A1ien51 didn't take care of you this, its simple.

} else if(x==2) { var k = document.getElementsByName('keyword');

for(i=0;i<k.length;i++){ k[i].checked = false;}
Copy linkTweet thisAlerts:
@SwirlykingauthorSep 01.2009 — thanks again, Justin!!
×

Success!

Help @Swirlyking 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 6.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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...