0

I want to toggle a button(display) as show hide based on the drop down values, however when I click another button(search) the page reloads and the button(display) if earlier visible is set to hide as the toggle function I have written on .change function. I want the button(display) to be visible even after the page reloads

This is my cshtml code

<div id = "search-container">
@using (Html.BeginForm("Search","Home",Formthod.Post,new{id="values"})){
<table>
<tr>
<td>
@Html.DropDownListFor(m=>m.Search.clientID,Model.ClientList,"All",
new{id = "sp-client"}
</td>
</tr>
<tr>
 <input type ="submit" value="search" id="search" value="search"/>
 <input type ="button" value="display" id="display" value="display"/>
</tr>

}
<script>
$('#sp-client').change{ function(){
if($client.val()==32)
{$("display").show()}
else
{$("display").hide()}
}
}
</script>

Any advice would be helpful

  • You need to enable post back in the control and check if page is post back [How can I check for IsPostBack in JavaScript?](https://stackoverflow.com/questions/59719/how-can-i-check-for-ispostback-in-javascript) – Black Frog May 10 '19 at 18:20
  • Possible duplicate of [How to detect/track postback in javascript?](https://stackoverflow.com/questions/1857606/how-to-detect-track-postback-in-javascript) – Black Frog May 10 '19 at 18:21

2 Answers2

0

If the page reloads and you want an element to be visible right away, just reference it and use .show() at the top of your script, outside of your functions. That way when the script loads upon refresh the element is instructed to be visible. If it does not work, you can encase the .show function for the element in $(document).ready(function(){Show code here}); at the top or bottom of your script.

0

to toggle a button(display) as show hide based on the drop down values:

$( "#p-client" ).change(function() {
if($client.val()===32)
{$("#display").show()}
else
{$("#display").hide()}
});
Murtaza Hussain
  • 3,851
  • 24
  • 30