2

Guys i want to ask how can i make a function in jQuery when i click a button to trigger a select of an option in a select menu.

Here is my HTML code:

<select id="target_menu" name="sort_by">
    <option selected="selected" id="position" value="http://cosmeo.superweb.bg/index.php/priceslider/slider/view/?dir=asc&amp;id=35&amp;max=424&amp;min=0&amp;order=position">Позиция</option>
    <option id="name" value="http://cosmeo.superweb.bg/index.php/priceslider/slider/view/?dir=asc&amp;id=35&amp;max=424&amp;min=0&amp;order=name">Име</option>
    <option id="price" value="http://cosmeo.superweb.bg/index.php/priceslider/slider/view/?dir=asc&amp;id=35&amp;max=424&amp;min=0&amp;order=price">Цена</option>
    <option id="color" value="http://cosmeo.superweb.bg/index.php/priceslider/slider/view/?dir=asc&amp;id=35&amp;max=424&amp;min=0&amp;order=color">Color</option>
    <option id="created_at" value="http://cosmeo.superweb.bg/index.php/priceslider/slider/view/?dir=asc&amp;id=35&amp;max=424&amp;min=0&amp;order=created_at">Дата</option>
</select>

The option which i want to be triggered as selected is created_at.

Here is the code of the button that i want when it is clicked to select option with id created_at of select menu with id target_menu:

<button onclick="triggerChange()" class="FirstFilter">
    Click me!
</button>

When i click on the button -> Click Me! i want jQuery to force/trigger a selection of option with id created_at at select menu with id target_menu.

How my function triggerChange() must look like ?

So guys, how can i make it?

Venelin
  • 2,905
  • 7
  • 53
  • 117

6 Answers6

3

Try this function:

 function triggerChange(){
        $("#target_menu").val($("#target_menu #created_at" ).val());
        $('#target_menu').trigger('change');
   }
PHP Worm...
  • 4,109
  • 1
  • 25
  • 48
  • Okey but does it change and select the option with id `created_at` ? – Venelin Mar 23 '15 at 14:41
  • I have updated the ans. according to your requirements – PHP Worm... Mar 23 '15 at 14:48
  • What you wrote works only when you manually change the select element and no matter what a user will choose it will select option with 'created_at' id. He wanted to set 'created_at' as a selected option on button click. How your answer helps him ? – Cosmin Mar 23 '15 at 15:09
1

There are few ways of doing it. One is below. A piece of advise is that do not use inline event handlers!

$("button").on("click", function() {
    $("#target_menu option").filter(function(opt, el) {
        return el.id === 'created_at' && $(el)
    }).prop("selected", true);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="target_menu" name="sort_by">
    <option selected="selected" id="position" value="http://cosmeo.superweb.bg/index.php/priceslider/slider/view/?dir=asc&amp;id=35&amp;max=424&amp;min=0&amp;order=position">Позиция</option>
    <option id="name" value="http://cosmeo.superweb.bg/index.php/priceslider/slider/view/?dir=asc&amp;id=35&amp;max=424&amp;min=0&amp;order=name">Име</option>
    <option id="price" value="http://cosmeo.superweb.bg/index.php/priceslider/slider/view/?dir=asc&amp;id=35&amp;max=424&amp;min=0&amp;order=price">Цена</option>
    <option id="color" value="http://cosmeo.superweb.bg/index.php/priceslider/slider/view/?dir=asc&amp;id=35&amp;max=424&amp;min=0&amp;order=color">Color</option>
    <option id="created_at" value="http://cosmeo.superweb.bg/index.php/priceslider/slider/view/?dir=asc&amp;id=35&amp;max=424&amp;min=0&amp;order=created_at">Дата</option>
</select>

<button class="FirstFilter">
    Click me!
</button>
lshettyl
  • 8,166
  • 4
  • 25
  • 31
1

function triggerChange()
{
  $("#target_menu").val($("#created_at").val());
}
<script src="http://code.jquery.com/jquery-latest.min.js"
        type="text/javascript"></script>
<select id="target_menu" name="sort_by">
    <option selected="selected" id="position" value="http://cosmeo.superweb.bg/index.php/priceslider/slider/view/?dir=asc&amp;id=35&amp;max=424&amp;min=0&amp;order=position">Позиция</option>
    <option id="name" value="http://cosmeo.superweb.bg/index.php/priceslider/slider/view/?dir=asc&amp;id=35&amp;max=424&amp;min=0&amp;order=name">Име</option>
    <option id="price" value="http://cosmeo.superweb.bg/index.php/priceslider/slider/view/?dir=asc&amp;id=35&amp;max=424&amp;min=0&amp;order=price">Цена</option>
    <option id="color" value="http://cosmeo.superweb.bg/index.php/priceslider/slider/view/?dir=asc&amp;id=35&amp;max=424&amp;min=0&amp;order=color">Color</option>
    <option id="created_at" value="http://cosmeo.superweb.bg/index.php/priceslider/slider/view/?dir=asc&amp;id=35&amp;max=424&amp;min=0&amp;order=created_at">Дата</option>
</select>

<button onclick="triggerChange()" class="FirstFilter">
    Click me!
</button>

Here is an answer to your question.

function triggerChange()
{
  $("#target_menu").val("created_at");
}
PrakashG
  • 1,642
  • 5
  • 20
  • 30
Rahul Rahatal
  • 648
  • 5
  • 19
0

as you have an id applied to each option you can target that id then use the .prop() method.

$(document).ready(function(){
 $(".firstFilter").click(function(){
    var id = $("#created_at").prop("selected","selected");   
 });
});
jrod
  • 66
  • 1
  • 6
0

Use this

function triggerChange(){    
$("#target_menu").eq(4).prop("selected","selected");     
}
I'm Geeker
  • 4,601
  • 5
  • 22
  • 41
-1

I made this fiddle for you

Fiddle

First step is to get value from element with id created_at:

var wantedValue= $('#created_at').attr('value');

And next, you need to set selected value for your select element:

$('#target_menu').val(wantedValue).trigger('change');  // need to call .trigger('change') here
// you can also call .trigger() - without 'change' keyword

Explanation about why you need to trigger .change HERE

For select boxes, checkboxes, and radio buttons, the event is fired immediately when the user makes a selection with the mouse, but for the other element types the event is deferred until the element loses focus.

Update ( thanks LShetty )

$('.FirstFilter').click(function(){
       $("#position").removeAttr("selected");
       $("#created_at").attr('selected','selected');
    });
Community
  • 1
  • 1
Cosmin
  • 2,184
  • 21
  • 38
  • Why trigger `change` when there is no capture? – lshettyl Mar 23 '15 at 15:02
  • Before giving -1 search : http://stackoverflow.com/questions/4672505/why-does-the-jquery-change-event-not-trigger-when-i-set-the-value-of-a-select-us – Cosmin Mar 23 '15 at 15:06
  • 1
    Well, I didn't give -1 by the way! You're consfusing `change` to setting a selected value. What OP wants is to set the selected value by clicking on a button! So, `change` is irrelevant here and `change` is not even bound to the `select` – lshettyl Mar 23 '15 at 15:12
  • Oh! Right! :) .. I understand what you're trying to say. My mind exploded when I read his question :)) – Cosmin Mar 23 '15 at 15:19