1

I want to click a certain option inside the div tag using Javascript .

Here is the HTML code:

<div class="sizes" data-info="product_sizes" id="select_size">
    <div id="sizes" style="display: block;">
        <select size="8" id="product_sizes">
            <option value="06.5" data-modelsize="06_5" data-sfs="false" class="">06.5</option>
            <option value="07.0" data-modelsize="07_0" data-sfs="false" class="">07.0</option>

I want to click the option value how would I go by doing this using Javascript.

1
  • Are you attempting to have one of the options be selected? If so: $(/*Whatever element you want*/).prop('selected', true); Commented Oct 2, 2013 at 18:21

3 Answers 3

5

Just target the select and set the value:

$("#product_sizes").val("06.5");

If you want to trigger the change event, add .change() to the above.

Sign up to request clarification or add additional context in comments.

Comments

0

You don't have to click it, just set the selectedIndex ir value of the <select>.

Without jQuery, this is how to do it:

var sel = document.getElementById('product_sizes');
sel.selectedIndex = 1; // selects 07.0
// Setting the value works too:
// sel.value = "07.0";

Comments

0

Try with:

/**
* OPT ID
*/
var opt = 3;
/**
* SELECTED
*/
var elements = $('#product_sizes option');
$(elements[ opt ]).attr("selected","selected").prop("selected",true);

/**
* OR SET VALUE
*/
$('#product_sizes').val( 19.5 );

/**
* CATCH
*/
$("#product_sizes").change(function(){
    console.log( $( this ).val() );
});

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.