Home:ALL Converter>Jquery select option as selected on change

Jquery select option as selected on change

Ask Time:2017-03-23T19:46:49         Author:Micheasl

Json Formatter

How can I Keep the two selected fields as select using a JavaScript/jquery function, if I pick a different language.

I know that STRG + mouselick works but I do not want that.

<fieldset class="form-group">
    <select class="form-control" multiple="multiple">
        <option value="bg">Bulgarisch</option>
        <option value="da">Dänisch</option>
        <option value="de" data-type="edit" selected="selected">Deutsch</option>
        <option value="en" data-type="edit" selected="selected">Englisch</option>
        <option value="et">Estnisch</option>
        <option value="fi">Finnisch</option>
    </select>
</fieldset>

https://jsfiddle.net/j280fhuu/

Author:Micheasl,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/42975131/jquery-select-option-as-selected-on-change
Anshul :

You can use mousedown event\n\nFIDDLE\n\nLike this:\n\n$('option').mousedown(function(e) {\n e.preventDefault();\n $(this).prop('selected', $(this).prop('selected') ? false : true);\n return false;\n});\n",
2017-03-23T11:57:14
Mustapha Larhrouch :

you can do it like this :\n\n$(\"#language_selection_audio\").change(function(){\n $(this).find(\"option[data-type='edit']\").prop('selected', true)\n})\n\n\nhttps://jsfiddle.net/j280fhuu/3/",
2017-03-23T11:53:48
Shadab Mehdi :

You can write like this\n\n$(document).ready(function () {\n $('#language_selection_audio').val(['de', 'en']); // Pass an array, for multiple selections\n $('#language_selection_audio :selected').attr('data-type', 'edit'); // Add custom attribute\n});\n\n\nJS Fiddle Here.",
2017-03-23T11:58:53
yy