0

Hi i tried auto populating my using this http://www.justsnaps.in/test?option=2 but it it not working.

html of the form

<select name="option" onchange="document.getElementById('youriframe').src = this.options[this.selectedIndex].value">
    <option>choice</option>
    <option value="http://thumbnails116.imagebam.com/49530/fb7601495291146.jpg" 
value="1">Image 1</option>
    <option value="http://thumbnails116.imagebam.com/49529/cbe8b5495287000.jpg" value="2">Image 2</option>
    <option value="http://thumbnails115.imagebam.com/49530/50d2e2495291163.jpg" value="3">Image 3</option>
</select>

<img id="youriframe" class="form-image" src="choise" alt="" border="0" />

Please post the correct Url for achieving this.

mplungjan
  • 169,008
  • 28
  • 173
  • 236

1 Answers1

0

Please note I removed the onchange from the inline and add it in the script. I also trigger the onchange if there is an option passed

function getParameterByName(name, url) {
  if (!url) url = window.location.href;
  name = name.replace(/[\[\]]/g, "\\$&");
  var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
    results = regex.exec(url);
  if (!results) return null;
  if (!results[2]) return '';
  return decodeURIComponent(results[2].replace(/\+/g, " "));
}
window.onload = function() { // when page loads
  var imgSel = document.getElementById("imgSel");
  imgSel.onchange = function() { // when changing
    var val = this.value; // get the value
    if (val) document.getElementById('yourImage').src = val;
  }

  var opt = getParameterByName("option"); // get the option from url
  if (opt) { // change options and trigger
    imgSel.selectedIndex = opt; 
    imgSel.onchange();
  }
}
<select name="option" id="imgSel">
  <option>choice</option>
  <option value="http://thumbnails116.imagebam.com/49530/fb7601495291146.jpg" value="1">Image 1</option>
  <option value="http://thumbnails116.imagebam.com/49529/cbe8b5495287000.jpg" value="2">Image 2</option>
  <option value="http://thumbnails115.imagebam.com/49530/50d2e2495291163.jpg" value="3">Image 3</option>
</select>

<img id="yourImage" class="form-image" src="" alt="" border="0" />
mplungjan
  • 169,008
  • 28
  • 173
  • 236