0

This is my drop down using select and the id is beh_type

<select class="form-control" id="beh_type">
<option value="0">Auto</option>
<option value="1">Man</option>
</select>

and this is my multiple select list and the id is mult_type

<select multiple="multiple" class="form-control" id="mult_type">
<option value="0">Auto</option>
<option value="1">Man</option>
</select>

and this is my JavaScript code for single click using if condition and the code to be executed for mult_type not for beh_type

if($("select").attr("id") == "mult_type"){        
$("select").mousedown(function(e){
        e.preventDefault();

            var select = this;
        var scroll = select.scrollTop;

        e.target.selected = !e.target.selected;

        setTimeout(function(){select.scrollTop = scroll;}, 0);

        $(select).focus();}
    }).mousemove(function(e){e.preventDefault()});
2
  • 1
    What is the problem here? Commented Aug 9, 2018 at 11:30
  • the JS should work for mult_type id only I.E the JS code is used to select multiple items in single click instead of ctrl click Commented Aug 9, 2018 at 11:33

2 Answers 2

2

If you need that event for select with id value as mult_type then add those event using the id selector:

$("#mult_type").mousedown(function(e){
    e.preventDefault();

    var select = this;
    var scroll = select.scrollTop;

    e.target.selected = !e.target.selected;

    setTimeout(function(){select.scrollTop = scroll;}, 0);

   $(select).focus();
}).mousemove(function(e){e.preventDefault()});
Sign up to request clarification or add additional context in comments.

2 Comments

works fine can i do this way $("#mult_type",#mult_type1").mousedown(function(e)
You could even use $("select#mult_type").mousedown to specify the select you want to use, but this just the id works as well. After all, I think the OP should learn more about jquery selectors: api.jquery.com/category/selectors
1

You just need to change the position of "if". The correct code would be

$("select").mousedown(function(e){
 if($("select").attr("id") == "mult_type"){    
    e.preventDefault();
    var select = this;
    var scroll = select.scrollTop;

    e.target.selected = !e.target.selected;

    setTimeout(function(){select.scrollTop = scroll;}, 0);

    $(select).focus();}
}).mousemove(function(e){e.preventDefault()});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.