• Advertisement
Sign in to follow this  

Game website JS question

This topic is 3155 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi my game website has a simple form on it that allows the users to choose between 4 radio buttons. When the user selects one of the buttons, I want a disabled section of the for to become enabled. The section consists of input and select tags only. Every input & select tag that is in this "disabled section" is assigned a class attribute with a value of "toggleSection". It would be nice to be able to use code like: document.getElementsByClass("toggleSection").enabled = true; However, I don't think JS support such a function; I'm only seeing getElementsById(), ...Name() or ...TagName(). What's the best approach here?

Share this post


Link to post
Share on other sites
Advertisement
It's been ages since I've done some JS, but what exactly is the problem with using document.getElementById()?

Share this post


Link to post
Share on other sites
If you really want to get elements by class, use something this:

document.getElementsByClass = function(cls) {
var found = [];
var rx = new RegExp('(^| )' + cls + '( |$)');
var els = this.getElementsByTagName('*');
for (var i=0; i<els.length; i++) {
var elClass = els.className;
if (rx.test(elClass)) {
found.push(elClass);
}
}
return found;
};

document.getElementByClass = function(cls) {
var rx = new RegExp('(^| )' + cls + '( |$)');
var els = this.getElementsByTagName('*');
for (var i=0; i<els.length; i++) {
var elClass = els.className;
if (rx.test(elClass)) {
return elClass;
}
}
return 0;
};





If you don't have elems using multiple classes like this
<div class="class1 class2">

You can replace the regex with a simple string comparison

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement