0

I have a disabled drop down that I only want enabled after it has been clicked. I have tried several methods, the following being the latest incarnation that doesn't work:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$(function() {

$("input:disabled").closest("div").click(function() {

$(this).find("input:disabled").attr("disabled", false).focus();

});

});

<div>
  <select name="test" id='testing' disabled>
<option name="first" value='first'>first</option>
<option name="second" value="second">second</option>
</select>
</div>
user1724365
  • 87
  • 2
  • 15

2 Answers2

1

$(function() {

 $('div > div').on('click', function(event) {

  $(this).hide().prev('select:disabled').removeAttr('disabled').focus();
  
 });

});
<html>
<head>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>

 <form action="" id="myForm">

  <div style="display:inline-block; position:relative;">
   <select name="test" id="testing" disabled>
    <option name="first" value='first'>first</option>
    <option name="second" value="second">second</option>
   </select>
    <div style="position:absolute; left:0; right:0; top:0; bottom:0;"></div>
  </div>

 </form>
</body>
</html>
chebaby
  • 7,362
  • 50
  • 46
1

This will detect the click on it by using CSS to add a layer over the select, but it still is not the best since it still requires more action from the user to open up the options. So it is basically a double click.

var elem = document.querySelector('span.cover');

function listener () {
  elem.classList.remove('active');
  elem.removeEventListener('mousedown', listener)
  var sel = elem.querySelector('select');
  sel.removeAttribute('disabled');
  sel.focus();
}


elem.addEventListener('mousedown', listener)
span.cover {
    position:relative;
}
span.active.cover:before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}
<span class="cover active">
  <select name="test" id='testing' disabled>
<option name="first" value='first'>first</option>
<option name="second" value="second">second</option>
</select>
</span>

If you want to do it with jQuery

$(document).on('click','span.active', function(){
  $(this).removeClass('active').find('select').removeAttr('disabled');
})
span.cover {
    position:relative;
}
span.active.cover:before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="cover active">
  <select name="test" id='testing' disabled>
<option name="first" value='first'>first</option>
<option name="second" value="second">second</option>
</select>
</span>

<span class="cover active">
  <select name="test" id='testing2' disabled>
<option name="first" value='first'>first</option>
<option name="second" value="second">second</option>
</select>
</span>
epascarello
  • 204,599
  • 20
  • 195
  • 236
  • Ok, but what about multiple selects in the same form. What would be the easiest way to handle that? The double click is perfectly acceptable. – user1724365 Aug 14 '18 at 21:17
  • So you loop over and add the events – epascarello Aug 14 '18 at 21:20
  • 1
    @user1724365 seems weird you picked the other answer that requires two elements to be added and not just adding the second element with CSS. Whatever, your code base.... – epascarello Aug 15 '18 at 12:50