Sign in to follow this  
plywood

Game website JS question

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
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[i].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[i].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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this