Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help for JS
#1
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
Reply
#2
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;
Reply
#3
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
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)