PDA

View Full Version : Help for JS



Noah
2009-09-13, 10:11 AM
Hello there, fellow maplers and programmers. Hopefully some of you guys may help me with this simple piece of 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:


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

Russt
2009-09-13, 12:46 PM
document.getElementById(id) gets the element with the specified id. Store it in a variable (say "foo") and you can just do

foo.disabled = !foo.disabled;

Noah
2009-09-13, 01:07 PM
Ah, thanks. I myself used this, as I name all the input-data in the forms. Seems to work fine in this way:


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

Noah