2

I am new to javascript however I am trying to create a if else statement with the following options.

<select name="remote_server" class="required">
<option></option>
<option value="1" <?php if($remote=="1") echo 'selected="selected"'; ?>>Yes</option>
<option value="0" <?php if($remote=="0") echo 'selected="selected"'; ?>>No</option>
</select>

I am looking to do something like

if remote_server = 1 then 
    document.getElementByName("hostname").disabled = true;
else 
    nothing

EDIT 1:

I am now trying the following with no success

var e = document.getElementsByName("remote_server");
var strUser = e.options[e.selectedIndex].value;

if ( strUser == 0 ) {
    document.getElementsByName("ftp_hostname").disabled = true;
}
3
  • What have you tried? Commented Dec 21, 2012 at 23:51
  • 1
    ive only tried some basic code ive found online to test it var remote_server = document.getElementByName("remote_server"); var selection = remote_server.options[remote_server.selectedIndex].value; if (selection == "1") { alert("selected!"); } Commented Dec 21, 2012 at 23:52
  • My code is exactly the same as yours. Then what's the problem? Commented Dec 21, 2012 at 23:53

1 Answer 1

1

getElementsByName returns an array of elements, so if you want the first element named "remote_server", you should use document.getElementsByName("remote_server")[0].

Similarly, you'll want to use document.getElementsByName("ftp_hostname")[0].disabled.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.