Is it possible in a html select
, to display the value of the option instead of the text?
It's gonna be used for displaying a long disription in the dropdown, but when selecting it should only be showing a short text/ the value of the option.
code:
<select>
<option value="1">This is a long long text, that explains option 1</option>
<option value="2">This is a long long text, that explains option 2</option>
</select>
Now the selected item, when you leave the dropdown/combobox/whatever, should only display '1'
We can change the HTML select element's selected option with JavaScript by setting the value property of the select element object. We have a select drop down with some options and 2 buttons that sets the selected options when clicked. We get the 2 buttons with getElementById .
select elements do not have a value attribute.
</option> , value="" is turned into just value . The value, when set to the empty string is simply discarded.
The value attribute specifies the value to be sent to a server when a form is submitted. The content between the opening <option> and closing </option> tags is what the browsers will display in a drop-down list. However, the value of the value attribute is what will be sent to the server when a form is submitted.
You can do it by using the label tag:
<option value="the_value" label="the text displayed">the hidden text</option>
This is the solution.
$("select option").each(function () {
$(this).attr("data-label", $(this).text());
});
$("select").on("focus", function () {
$(this).find("option").each(function () {
$(this).text($(this).attr("data-label"));
});
}).on("change mouseleave", function () {
$(this).focus();
$(this).find("option:selected").text($(this).val());
$(this).blur();
}).change();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With