C
C#16mo ago
XD

❔ How do I hide or show a div based on the value of a property of my selected option?

When I select a habit type, that habit type has a Measurability property, and I want to show or hide divs based on the value of this property. How can I do it?
<form method="post">
<label asp-for="HabitLog.HabitTypeName" class="control-label">Select the habit to log</label>
<select asp-for="HabitLog.HabitTypeName" class="form-control">
<option value=" ">Select an option</option>
@foreach (var habitType in Model.HabitTypes)
{
<option value="@habitType.Name">@habitType.Name</option>
}
</select>
<br />
*Other fields*
<form method="post">
<label asp-for="HabitLog.HabitTypeName" class="control-label">Select the habit to log</label>
<select asp-for="HabitLog.HabitTypeName" class="form-control">
<option value=" ">Select an option</option>
@foreach (var habitType in Model.HabitTypes)
{
<option value="@habitType.Name">@habitType.Name</option>
}
</select>
<br />
*Other fields*
public class HabitType
{
public int Id { get; set; }

public string ImagePath { get; set; }

public string Name { get; set; }

public string Measurability { get; set; }

public string UnitOfMeasurement { get; set; }
}
public class HabitType
{
public int Id { get; set; }

public string ImagePath { get; set; }

public string Name { get; set; }

public string Measurability { get; set; }

public string UnitOfMeasurement { get; set; }
}
By the way, the reason for which I don't want to do it with the name/value of the selected option, is because I would have to hardcode these options in a JavaScript function, but the user is able to add new habit types.
8 Replies
Angius
Angius16mo ago
You want to do that dynamically? Without page refresh? Or on page load?
XD
XDOP16mo ago
Sorry but could you explain me which would be the difference? I want the divs to remain hidden until the user selects an option
Angius
Angius16mo ago
Well... the difference is page reload or no page reload
XD
XDOP16mo ago
If there was a page reload wouldn't the selected option data be lost?
Angius
Angius16mo ago
Depends how you handle it
XD
XDOP16mo ago
Well, with no page reload then
Angius
Angius16mo ago
Use JS
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?