Reputation: 752
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
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
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
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