I have the following drop down list on my page. I also have a onchange event if someone selects an item from the drop down list. I can see the item index in the onchange event:
@(Html.Kendo().DropDownList()
.Name("Awards")
.Label(label => label.Content("Please select Award type from the list. "))
.DataTextField("AwardCategory1")
.DataValueField("AwardId")
.OptionLabel("Select award")
.DataSource(source =>
{
source.Read(read =>
{
read.Action("GetAwards", "Home");
});
})
.Events(e => e.Change("onChange"))
)
This is my onChange event:
<script>
function onChange(e) {
var dropdownlist = $("#Awards").data("kendoDropDownList");
var text = dropdownlist.text();
$(".award").text(text);
alert(text);
}
</script>
In the alert, I can see the selected item from the dropdown. I want to display some text on my web page based on the selected item so if text =="Award1" then I want to display
if (text =="Award1" )
<h3>"This is the award text for several people. Everyone will be given Award "A"></h3>
else if (text =="Award2")
<h3>Test this award. Please make sure, you will be on time</h3>
else if (text =="Award3")
.
.
and so on.
How can I show the "<h3>"
tag text on my web page based on the drop down list value. I can successfully capture the drop down list text value in JavaScript, but how can I display a certain text based on the selected item. I tried to put this line in my Javascript function:
$(".award").text(text);
so that I can capture this in tag like this:
<span class="award"></span>
with the above line, I can see the selected award, but I cannot display different text on my web page when a different item is selected. I dont need help with Telerik dropdown, I need help with javascript part.
any help is appreciated.
I tried the solution given below:
function onChange() {
var dropdownlist = $("#Awards").data("kendoDropDownList");
var text = dropdownlist.text();
//$(".award").text(text);
const award = {
"Award1": `<h3>This is the award text for several people. Everyone will be given Award "A"></h3>`,
"Award2": `<h3>Test this award. Please make sure, you will be on time</h3>`,
"Award3": `<h3>Blablabla</h3>`,
}[text]
$(".test").text(award);
//return { awardText: text };
}
I have this on the top of my page:
<div id="test">
</div>
2
Answers
Try this:
One option, like you lay out yourself, is to change the innerHTML of a div when you select something.
Another option is to have all the texts already in the document and then hide and show them. In this example I use the subsequent-sibling combinator to style the h3 so that is shows. This is a bit tricky to use because the selectors on each side of the
~
need to be on the same level in the DOM tree.