1

For some reason the code below it is not working correctly. Unless I'm being quite stupid with my JavaScript I can't see what's going wrong besides the onclick events not firing on the <option>s.

function showOther() {
  document.getElementById('other').value = "";
  document.getElementById('other').style.display = 'block';
  document.getElementById('otherBR').style.display = 'block';
}

function hideOther() {
  document.getElementById('other').style.display = 'none';
  document.getElementById('otherBR').style.display = 'none';
}
#other {
  display: none;
}
#otherBr {
  display: none;
}
<select name="" id="drop_down">
  <option value="choose" onclick="hideOther();">Please choose</option>
  <option value="Allure" onclick="hideOther();">Allure</option>
  <option value="Elle" onclick="hideOther();">Elle</option>
  <option value="In-Style" onclick="hideOther();">In-Style</option>
  <option value="other" id="otherOption" onclick="showOther();">Other</option>
</select>
<input type="text" name="fields_where" id="other" placeholder="Other" />
<br id="otherBR" />

8
  • 1
    You'll need to hook onto the select element instead. Commented Jan 9, 2014 at 19:59
  • 4
    Use the "onchange" event. The options should be considered part of the parent select and not separate controls. Commented Jan 9, 2014 at 19:59
  • 1
    @laaposto OP's example doesn't use jQuery... Commented Jan 9, 2014 at 19:59
  • @BrianDriscoll I can use jQuery if necessary. I'm just new to it so I didn't want to look all of that up as the client project is due tomorrow. Commented Jan 9, 2014 at 20:01
  • If you have an answer then post it as an answer, not as a comment! Commented Jan 9, 2014 at 20:01

4 Answers 4

3

Add this function to your JS:

function showHideOther(){
    if (document.getElementById('drop_down').value == 'other') {
         showOther();   
    } else {
         hideOther();   
    }
}

And change your select element like this:

<select name="" id="drop_down" onchange="showHideOther();">
        <option value="choose">Please choose</option>
        <option value="Allure">Allure</option>
        <option value="Elle">Elle</option>
        <option value="In-Style">In-Style</option>
        <option value="other">Other</option>
</select>

function showHideOther() {
  if (document.getElementById('drop_down').value == 'other') {
    showOther();
  } else {
    hideOther();
  }
}

function showOther() {
  document.getElementById('other').value = "";
  document.getElementById('other').style.display = 'block';
  document.getElementById('otherBR').style.display = 'block';
}

function hideOther() {
  document.getElementById('other').style.display = 'none';
  document.getElementById('otherBR').style.display = 'none';
}
#other {
  display: none;
}
#otherBr {
  display: none;
}
<select name="" id="drop_down" onchange="showHideOther();">
  <option value="choose">Please choose</option>
  <option value="Allure">Allure</option>
  <option value="Elle">Elle</option>
  <option value="In-Style">In-Style</option>
  <option value="other">Other</option>
</select>
<input type="text" name="fields_where" id="other" placeholder="Other" />
<br id="otherBR" />

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

3 Comments

It works in jsFiddle if you change the load option to No wrap... see here: jsfiddle.net/gLML3/22
Great, and for clarification on implementation what does No Wrap mean in terms of using it on your own site. As in should that code not go in the header? Or the body? Or something else entirely?
It means that the function declarations are not wrapped in other code, such as window.onLoad = function() { function showHideOther(){...} }
2

An answer with JS only, not jQuery:

onclick event in option tag is just recognized by Firefox. If you need a solution that works on all browsers such as IE or Chrome you can use onchange event on your "Select" element.

HTML :

<select name="" id="drop_down" onchange="showHideOther(this.value);">
    <option value="choose" ">Please choose</option>
    <option value="Allure">Allure</option>
    <option value="Elle" >Elle</option>
    <option value="In-Style" >In-Style</option>
    <option value="other" id="otherOption">Other</option>
</select>

JS defined in the html head section as script:

function showHideOther(value){
    alert(value);
    if (value==='other'){
        document.getElementById('other').value = "";
        document.getElementById('other').style.display = 'block'; 
        document.getElementById('otherBR').style.display = 'block';
    }
    else{
            document.getElementById('other').style.display = 'none'; 
            document.getElementById('otherBR').style.display = 'none';
    }
}

JSFiddle sample working fine: http://jsfiddle.net/amontellano/gLML3/14/

I hope it helps.

3 Comments

I don't see how your code is working at all. You have errors in the HTML and showOther is never called.
I'm answering also that you need to use onchange event in select element instead of onclick inside your options. I think the current sample does what you need now my friend.
@user3179156 My sample is working fine in JSFiddle. Try it if you want.
0

DEMO

var dropdown = document.getElementById('drop_down');

dropdown.onchange = function() {
  var selected = dropdown.options[dropdown.selectedIndex].value;

  switch(selected) {
    case 'other':
      document.getElementById('other').value = "";
      document.getElementById('other').style.display = 'block'; 
      document.getElementById('otherBR').style.display = 'block';
      break;
    default:
      document.getElementById('other').style.display = 'none'; 
      document.getElementById('otherBR').style.display = 'none';
      break;
  }
}

Comments

-2

just add these function to your code:

$('#drop_down').on('change', function(){
    ($(this).val() == 'other') ? showOther() : hideOther();
});

se here: http://jsfiddle.net/gLML3/7/

1 Comment

This would require the use of jQuery, which OP is not using.

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.