I want to change the selected option of a <select>
dropdown using a element outside of the <select>
. I’m using WordPress, and the select functionality is under this function: http://codex.wordpress.org/Function_Reference/selected
I tried a simple jQuery function:
<select id="bto_item_options_<?php echo $group_id; ?>" name="bto_selection_<?php echo $group_id; ?>" class="selectDD">
[Loop details]
<option data-title="<?php echo get_the_title( $product_id ); ?>" value="<?php echo $product_id; ?>" <?php echo selected( $selected_value, $product_id, false ); ?> ><?php echo get_the_title( $product_id ); ?></option>
[Loop end]
</select>
[Loop details]
<div id="<?php echo $product_id; ?>" onclick="selectOptionDD(<?php echo $product_id; ?>)" ><?php echo get_the_title( $product_id ); ?></div>
[Loop end]
<script type="text/javascript">
function selectOptionDD(title) {
$(".selectDD").val(title);
}
</script>
This code worked, yet just with the html element, didn’t work with the wordpress function, the functionality of everything else is under the selected WordPress function.
Is there a way to use this function outside of the <select>
element?
You can use raw javascript to change a select element option, but you may need to change your implementation slightly. Simply access the select element using the id attribute and then determine the index of the option element you would like to select.
EDIT
I think I misunderstood exactly what you are trying to do. If I am getting this right, what you want is to be able to do is click a
<div>
with some text in it, which will then select the appropriate<option>
in a<select>
box matching the text from your<div>
. Hopefully I am understanding that is what you want to do. If so, then try this function out:Next you will need to pass the function the
<div>
element with the text and the<select>
element to operate on.That should get you what you need. Hope that helps, or at least gets you going in the right direction.
Try this one:
you don’t need fire onclick function to all div tag:
Thanks.
I think what you want is selecting a select option with a button outside the select box element. Well you can use this on any html with Jquery:
Tested this and it will work as you want.
jsfiddle
You could pass group id value along with product id and set the value through getting element by id rather than by class
something like this…
Hope it helped…
What I have understand so far, there is no issue with this code
and as you said somehow following line is not working.
and what you asked for
So when you click on the div element it will call the function
selectOptionDD
in the javascript, but when page loads it will not show the last selected item that is may be saved in the database and currently it has in$selected_value
php
variable? If this is the case what I can suggest you is run the following code at the end of your page.I hope this will help, I understood your question correctly..