wharfdale
wharfdale

Reputation: 752

Choose default image to load on page load

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>

Upvotes: 0

Views: 169

Answers (3)

Sajad Karuthedath
Sajad Karuthedath

Reputation: 15767

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" })); });

Upvotes: 1

Jason Loki Smith
Jason Loki Smith

Reputation: 438

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);
});

Upvotes: 0

Swarna Latha
Swarna Latha

Reputation: 1004

Just set selected attribute as below.

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

Upvotes: 0

Related Questions