Southperry.net
Help for JS - Printable Version

+- Southperry.net (https://www.southperry.net)
+-- Forum: Social (https://www.southperry.net/forumdisplay.php?fid=14)
+--- Forum: Rubik's Cube (https://www.southperry.net/forumdisplay.php?fid=58)
+--- Thread: Help for JS (/showthread.php?tid=16283)



Help for JS - Noah - 2009-09-13

Hello there, fellow maplers and programmers. Hopefully some of you guys may help me with this simple piece of code:

Code:
function disable_enable(){
    document.form.text.disabled= !document.form.text.disabled;
}

What I want to do, is to be able to use this function on several elements. A simple example of what I want the function to do is this:

Code:
function disable_enable(id){
    document.form.id.disabled= !document.form.id.disabled;
}

Where id is a thing you're allowed to change as you wish. Understand what I mean?

So now the issue is that I don't know how to do this in javascript, as I'm no good with it. Anyone that want to help me with this? Thanks! Chin

Noah


Help for JS - Russt - 2009-09-13

document.getElementById(id) gets the element with the specified id. Store it in a variable (say "foo") and you can just do
Code:
foo.disabled = !foo.disabled;



Help for JS - Noah - 2009-09-13

Ah, thanks. I myself used this, as I name all the input-data in the forms. Seems to work fine in this way:

Code:
function enable_disable(str){
    var x = document.getElementsByName(str)[0];
    x.disabled = !x.disabled;
}

Noah