-2

i have a following select list

<select id = 'status-list'>
  <option value = 'all'>All</option>
  <option value = 'active'>Active</option>
  <option value = 'inactive'>Inactive</option>
</select>

I want to select option Active every time page loads. I have used to set required option by using following code

$(document).ready(function() {  
  $("#status-list option[value='active']").prop("selected", true);
});

But the issue is every time i changed option page get reload and every

Rajan
  • 27
  • 6
  • It's not clear what the problem is, and you seem to have left the question unfinished. In the title you don't want this code to run every time the page loads, but in the question you say that you do. Can you clarify? – David Jan 24 '17 at 18:04
  • Whenever i choose any option from select list page reloads. So i added code on ready so every time it shows Active as selected. I want to set Active only on first visit. After that whatever user select it should be selected. – Rajan Jan 24 '17 at 18:19
  • Why does the page reload? Whatever you're doing when you reload the page, if you want to retain the state of the page then you need to persist that state somewhere. For example, if this `select` is part of a `form` being posted then the server-side code handling the form post can set the value for the `select` (and can conditionally output the code you currently have to set the default value, or remove that code completely and set it server-side). The example doesn't include a lot of what's happening here. You're setting the value on page load and loading the page, so it's working as designed. – David Jan 24 '17 at 18:23
  • I have used php for creating select list as follows: $products = array('all' => 'All', 'active'=>'Active', 'inactive'=>'Inactive'); foreach($products as $key => $item) { if ($inngn_status && $inngn_status == $key) { $out .= ""; } else { $out .= ""; } } – Rajan Jan 24 '17 at 18:25
  • Moreover, i sends selected value through query string – Rajan Jan 24 '17 at 18:26
  • It's still not clear why the page is automatically reloading in the first place. But if you have the value that you want to set in the `select` then use that value when the page loads. As you've discovered, if you *always* set the same value then the same value will *always* be set. – David Jan 24 '17 at 18:28
  • i have pass the value of select list through query string so page reload whenever selection made in select list. – Rajan Jan 24 '17 at 18:30
  • Then in your JavaScript code you can check for that value on the query string. If it's there, use it. If it isn't, use your default value ("active"). This should help: http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript – David Jan 24 '17 at 18:32
  • initially there is no value in query string ... can this work ..$("#status-list option[value='active']").prop("selected", true).trigger("change"); – Rajan Jan 24 '17 at 18:38
  • And in that case you'd use the hard-coded default value that you already have. Can that "work" for what? What are you trying to accomplish with that line of code? – David Jan 24 '17 at 18:39
  • $("#status-list option[value='active']").prop("selected", true).trigger("change"); will this code of line trigger selection of select list ? means will user able to change select options and retain user selection after page reload ? – Rajan Jan 25 '17 at 03:07

1 Answers1

0

Try this

$(document).ready(function() { 
 $("#status-list").val("active");
});
Manoj Lodhi
  • 978
  • 4
  • 10
  • Like this $(document).ready(function() { $("#status-list").val("active"); }); – Rajan Jan 24 '17 at 18:06
  • yes, it will set the value active when page will load. – Manoj Lodhi Jan 24 '17 at 18:08
  • yeah it sets value but issue is that when user selects other value page reload and it again set Active. I only require Active only when user first time visit this page. After that selection will user specific. – Rajan Jan 24 '17 at 18:14
  • then you have to preserve the last selection, you can use local storage for that. – Manoj Lodhi Jan 24 '17 at 18:22