0

I have a dropdown of which changes the image within the defined DIV. When the page loads, no image is selected by default, only when you choose a dropdown option does it show. I would like it to choose the first option from the list on page load.

<div id="eyelash_style_img"></div>

<select id="eyelash_style_select" name="os0">
  <option value="select">-Choose a Style-</option>
  <option value="L004">L004 </option>
  <option value="L005">L005 </option>
  <option value="L010">L010 </option>
  <option value="L021">L021 </option>
  <option value="L024">L024 </option>
  <option value="L038">L038 </option>
</select>

<script type="text/javascript">

$("#eyelash_style_select").change(function() {  
 $("#eyelash_style_img").html($("<img />", { src: "<?=HTTP_HOST;?>Shopping/Images/" + $(this).val() + ".jpg" })); });

</script>

3 Answers 3

1

Use selected attribute

<select id="eyelash_style_select" name="os0">
        <option value="select">-Choose a Style-</option>

     <option value="L004"  selected>L004 </option>
        <option value="L005">L005 </option>
        <option value="L010">L010 </option>
        <option value="L021">L021 </option>
        <option value="L024">L024 </option>
        <option value="L038">L038 </option>
    </select>

Javascript:

 $(document).ready(function () {
(
$("#eyelash_style_img").html($("<img />", { src: "<?=HTTP_HOST;?>Shopping/Images/" + $(this).val() + ".jpg" })); });
});

this script loads the image after pageLoad,just added the change function in document.ready to load the default image that is selected,you need both

$("#eyelash_style_select").change(function() {  
 $("#eyelash_style_img").html($("<img />", { src: "<?=HTTP_HOST;?>Shopping/Images/" + $(this).val() + ".jpg" })); });
Sign up to request clarification or add additional context in comments.

1 Comment

That selects the option in the dropdown but doesn't automatically load the image too.
0

Just set selected attribute as below.

<option value="1" selected="selected">1</option>
<option value="2">2</option>
<option value="3">3</option>

Comments

0

Create a page onload event and select the first option in the list once the page has loaded.

$(document).ready(function () {
(
    $("#eyelash_style_select").prop('selectedIndex', 0);
});

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.