RAUSHAN KUMAR
RAUSHAN KUMAR

Reputation: 6006

Material-Design the selected option is not getting displayed

I am using materlize. The problem is i have to make an option selected using jquery. All working fine when i am trying to get the value of selected option. But the only problem is the selected ption is not getting highligted. It always shown the default option text "Choose An Option"

My Html code is

<select id="redirect_select" name="redirect_select" onchange="showRedirect();">
    <option value="">Choose An Option</option>
    <option value="0">Use Redirect URL</option>
    <option value="1">Use Success Message</option>
</select>

And my script code is

$("#redirect_select").val("0").trigger("onchange");

Upvotes: 0

Views: 406

Answers (1)

TSR
TSR

Reputation: 20590

What you have to do is to:

  1. Get the value first
  2. Then change the select option after using val("0") without trigger

function showRedirect(){
val = $("#redirect_select").val();
  alert("value is: "+val)
$("#redirect_select").val("0");

  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="redirect_select" name="redirect_select" onchange="showRedirect();">
    <option value="">Choose An Option</option>
    <option value="0">Use Redirect URL</option>
    <option value="1">Use Success Message</option>
</select>

Upvotes: 2

Related Questions