I’m relatively new to jQuery and need some help to fix an issue i have.
I wrote this script to show a quantity box when someone selects an option from a drop down select list. The client requested a “Reset Selection” link next to the drop down which i have.
The problem is that if someone makes a selection, but then changes their mind and choses a different option, the slideToggle is triggered again which then hides the quantity box and add to cart button.
Is there any way to make this function so that it will only slideToggle closed if the first “select option” choice is clicked, or the “reset” link, as opposed to any other option.
Hope that makes sense.
<script language="javascript">
jQuery.noConflict();
<!--
// prepare the form when the DOM is ready
jQuery(document).ready(function() {
jQuery(".expcontent").hide();
jQuery(".reset").hide();
jQuery(".back").change(function(){
jQuery(".expcontent").slideToggle(800);
jQuery(".reset").show();
});
jQuery(".reset").click(function(){
jQuery(".reset").hide();
jQuery(".expcontent").slideToggle(800);
jQuery('.back option:selected').removeAttr('selected');
});
});
</script>
Based on the responses given below, i have tried:
jQuery(".back").change(function(){
var sel=jQuery('.back option:first')
if (sel)
jQuery(".expcontent").slideUp(800);
else
jQuery(".expcontent").slideDown(800);
but slideDown never occurs using this so i’m assuming the condition for var sel isn’t being met. Is it even a valid condition? I’m struggling to find much info on how to tell if the first option in a “select” drop down has been chosen.
Worked it out. replaced
var sel=jQuery(".back option:first")
with
var sel=jQuery(".back option:first").attr('selected')
1 Answer