Reputation: 308
I want to change the visibility of divs depending on the option selected in a dropdown menu, I thought something like this would work
<select>
<option onclick="hideButton();" value="A">A</option>
<option onclick="showButton();" value="B">B</option>
<option onclick="showButton();" value="C">C</option>
</select>
I have one button that I want to change the visibility of
<input class="button" name="button" type="submit" value="delete" />
<script type="text/javascript">
function hideButton(){$(".button").hide("slow");}
function showButton(){$(".button").show("slow");}
</script>
of course it does nothing and I've no idea why, I thought the theory was sound but I just don't know enough about jquery
Upvotes: 1
Views: 342
Reputation: 14967
you can decide on the option
that action has to take:
Html:
<select>
<option data-visibility="hide" value="A">A</option>
<option data-visibility="show" value="B">B</option>
<option data-visibility="show" value="C">C</option>
</select>
Js (jQuery):
$('select').bind('change', function(event) {
var method = $(this).find('option:selected').data('visibility');
$('.button')[method]('slow');
});
Upvotes: 0
Reputation: 55740
$(function() {
$('select').on('change', function() {
if ($(this).val() == 'A') {
$('.button').hide('slow');
}
else {
$('.button').show('slow');
}
}).change();
});
<select>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
<input class="button" name="button" type="submit" value="delete" />
Upvotes: 0
Reputation: 1142
Try this:
$('select').change(function(){
if($(this).val() == 'A'){
hideButton();
}else{
showButton();
}
})
Upvotes: 1