pihu
pihu

Reputation: 120

How to reset dropdown field using javascript

Hi I have tried below code to reset number of "adults" dropdown field on onclick of reset button but the dropdown field is not getting reset please suggest

Code for adult dropdown :

<select class="select_style sel_ad_{$smarty.section.sect.iteration}" id="adult[]" 
name="adult[]"
onchange="javascript:display_twin('{$smarty.section.sect.iteration}',this.value)">
<option value="0"> - </option>
{section name=adult loop=10 start=1 step=1} 
<option value="{$smarty.section.adult.index}" {if $adults_details.$sect_key eq 
 $smarty.section.adult.index}selected{/if}>{$smarty.section.adult.index} </option
 {/section} 
 </select>

Javascript code for reseting fields onclick of reset button :

 function reset_frm(){
    $("#adults_pkg").val('');
    //$("select[name='adults_pkg[0]']").val('');
 }

Upvotes: 1

Views: 1433

Answers (3)

Sougata Bose
Sougata Bose

Reputation: 31749

Try with -

$("yourSelectMenu").prop('selectedIndex',0);

Upvotes: 0

Girish
Girish

Reputation: 12127

Default mean selected first option of select value, as you have have 0 value on first index so you will need to assign 0 for default index

And selector seems wrong, support the select box inside loop so use right selector like

        $(".sel_ad_{$smarty.section.sect.iteration}").val('0');

Upvotes: 0

Pupil
Pupil

Reputation: 23958

You are passing wrong id.

To use existing code:

$('#adult\\[\\]').val('');

OR

Add a new class to the element:

<select class="pkgCls select_style sel_ad_{$smarty.section.sect.iteration}" id="adult[]" 
name="adult[]"
onchange="javascript:display_twin('{$smarty.section.sect.iteration}',this.value)">

And apply this:

$(".pkgCls").val('');

Upvotes: 1

Related Questions