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=".php/priceslider/slider/view/?dir=asc&id=35&max=424&min=0&order=position">Позиция</option>
<option id="name" value=".php/priceslider/slider/view/?dir=asc&id=35&max=424&min=0&order=name">Име</option>
<option id="price" value=".php/priceslider/slider/view/?dir=asc&id=35&max=424&min=0&order=price">Цена</option>
<option id="color" value=".php/priceslider/slider/view/?dir=asc&id=35&max=424&min=0&order=color">Color</option>
<option id="created_at" value=".php/priceslider/slider/view/?dir=asc&id=35&max=424&min=0&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?
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&id=35&max=424&min=0&order=position">Позиция</option>
<option id="name" value="http://cosmeo.superweb.bg/index.php/priceslider/slider/view/?dir=asc&id=35&max=424&min=0&order=name">Име</option>
<option id="price" value="http://cosmeo.superweb.bg/index.php/priceslider/slider/view/?dir=asc&id=35&max=424&min=0&order=price">Цена</option>
<option id="color" value="http://cosmeo.superweb.bg/index.php/priceslider/slider/view/?dir=asc&id=35&max=424&min=0&order=color">Color</option>
<option id="created_at" value="http://cosmeo.superweb.bg/index.php/priceslider/slider/view/?dir=asc&id=35&max=424&min=0&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?
Share Improve this question asked Mar 23, 2015 at 14:30 VenelinVenelin 3,3067 gold badges71 silver badges149 bronze badges 1- stackoverflow./questions/4680075/… – errand Commented Mar 23, 2015 at 14:38
6 Answers
Reset to default 3Try this function:
function triggerChange(){
$("#target_menu").val($("#target_menu #created_at" ).val());
$('#target_menu').trigger('change');
}
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./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&id=35&max=424&min=0&order=position">Позиция</option>
<option id="name" value="http://cosmeo.superweb.bg/index.php/priceslider/slider/view/?dir=asc&id=35&max=424&min=0&order=name">Име</option>
<option id="price" value="http://cosmeo.superweb.bg/index.php/priceslider/slider/view/?dir=asc&id=35&max=424&min=0&order=price">Цена</option>
<option id="color" value="http://cosmeo.superweb.bg/index.php/priceslider/slider/view/?dir=asc&id=35&max=424&min=0&order=color">Color</option>
<option id="created_at" value="http://cosmeo.superweb.bg/index.php/priceslider/slider/view/?dir=asc&id=35&max=424&min=0&order=created_at">Дата</option>
</select>
<button class="FirstFilter">
Click me!
</button>
function triggerChange()
{
$("#target_menu").val($("#created_at").val());
}
<script src="http://code.jquery./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&id=35&max=424&min=0&order=position">Позиция</option>
<option id="name" value="http://cosmeo.superweb.bg/index.php/priceslider/slider/view/?dir=asc&id=35&max=424&min=0&order=name">Име</option>
<option id="price" value="http://cosmeo.superweb.bg/index.php/priceslider/slider/view/?dir=asc&id=35&max=424&min=0&order=price">Цена</option>
<option id="color" value="http://cosmeo.superweb.bg/index.php/priceslider/slider/view/?dir=asc&id=35&max=424&min=0&order=color">Color</option>
<option id="created_at" value="http://cosmeo.superweb.bg/index.php/priceslider/slider/view/?dir=asc&id=35&max=424&min=0&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");
}
Use this
function triggerChange(){
$("#target_menu").eq(4).prop("selected","selected");
}
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");
});
});
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');
});