Micheasl
Micheasl

Reputation: 287

Jquery select option as selected on change

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/

Upvotes: 0

Views: 442

Answers (4)

romuloinnocencio
romuloinnocencio

Reputation: 26

not sure if this will help; Try it! I can't understand clear what you want.

var $input = $('#language_selection_audio');

function markLangs( langs ){
    $input.find('option').removeAttr('data-type').attr('selected',false);

    $.each( langs, function( i ){
         $input.find('option[value=' + langs[i] + ']').attr('data-type','edit').attr('selected',true);              
    });             
};

$input.on('change', function(){
    markLangs( $(this).val() );
});

see ya.

Upvotes: 0

Shadab Mehdi
Shadab Mehdi

Reputation: 653

You can write like this

$(document).ready(function () {
   $('#language_selection_audio').val(['de', 'en']); // Pass an array, for multiple selections
   $('#language_selection_audio :selected').attr('data-type', 'edit'); // Add custom attribute
});

JS Fiddle Here.

Upvotes: 0

Anshul
Anshul

Reputation: 128

You can use mousedown event

FIDDLE

Like this:

$('option').mousedown(function(e) {
    e.preventDefault();
    $(this).prop('selected', $(this).prop('selected') ? false : true);
    return false;
});

Upvotes: 1

Mustapha Larhrouch
Mustapha Larhrouch

Reputation: 3393

you can do it like this :

$("#language_selection_audio").change(function(){
    $(this).find("option[data-type='edit']").prop('selected', true)
})

https://jsfiddle.net/j280fhuu/3/

Upvotes: 0

Related Questions