3

I got two select-fields selectA and selectB on my page. Now I'd like to preselect them by clicking a link: http://www.mypage.com/index.html?selectA=a132&selectB=b02#bestellung I manage decoding and writing a132 and b02 into var idA and var idB using javascript. But how do I use these var to trigger the preselection of the fields? a132 is the id of a selectA-option. b02 is the id of a selectB option.

My code:

<!-- decode url write vars -->
<script>
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&#]*)/gi, function(m,key,value) {
vars[key] = value;
});
return vars;
}
</script> 

<!-- set preselect -->
  <script>
var idA = getUrlVars()["selectA"];
var idB = getUrlVars()["selectB"];

document.getElementById("selectA").selectedId = idA;
document.getElementById("selectB").selectedId = idB;
</script>



<!-- update Image for documentation only -->
  <script>
function updateImage(id, srcCallback){
 var img = document.getElementById(id);
 if (img){
  img.src = srcCallback();
 }
}

function getImageSrc(form){
 var sel1 = form.Size;
 var sel2 = form.Options;
 return "img/preview/" + sel1.options[sel1.selectedIndex].id + sel2.options[sel2.selectedIndex].id + ".jpg";
}
</script>
<select id="selectA" name="Size" onchange="ReadForm (this.form, false);updateImage('image1', getImageSrc.bind(undefined, this.form));BeschreibungA();" required class="form-control">
  <option value="+0.00" id="a00" selected>1. Please select:</option>
    <option value="wei&szlig; +15.00 Eur" id="a130">wei&szlig; 15,00 EUR</option>
 <option value="wei&szlig; | gold +10.00 Eur" id="a131">wei&szlig; | gold 10,00 EUR</option>
 <option value="gr&uuml;n | gold +12.00 Eur" id="a132">gr&uuml;n | gold 12,00 EUR</option>
 <option value="antrazith +15.00 Eur" id="a133">antrazith 15,00 EUR</option>
 <option value="natur | gold +13.00 Eur" id="a134">natur | gold 13,00 EUR</option>
 <option value="UJack +18.00 Eur" id="a135">Union Jack 18,00 EUR</option>
 <option value="StG +18.00 Eur" id="a136">StG 18,00 EUR</option>
 <option value="Streifen +18.00 Eur" id="a137">Streifen 18,00 EUR</option>
 <option value="Gold +18.00 Eur" id="a138">Gold 18,00 EUR</option>
  </select>


<br>
<select id="selectB" name="Options" data-parent-id="selectA" onchange="ReadForm (this.form, false);updateImage('image1', getImageSrc.bind(undefined, this.form));BeschreibungB();" required class="form-control">>
  <option value="+0.00" id="b99" selected >2. Please select:</option>
  <option value="wei&szlig; +3.00 Eur inkl. Versand und 19% MWSt." id="b01" data-parent-option-ids="a00,a130,a131,a135">wei&szlig; (E14) 3,00 Eur</option>
  <option value="orange +3.00 Eur inkl. Versand und 19% MWSt." id="b02" data-parent-option-ids="a00,a130,a131,a132,a133,a134,a138">orange (E14) 3,00 Eur</option>
  <option value="pink +3.00 Eur inkl. Versand und 19% MWSt." id="b03" data-parent-option-ids="a00,a130,a131,a132,a133,a134,a138">pink (E14) 3,00 Eur</option>
  <option value="rot +3.00 Eur inkl. Versand und 19% MWSt." id="b04" data-parent-option-ids="a00,a130,a131,a132,a133,a134,a135,a136,a137,a138">rot (E14) 3,00 Eur</option>
  <option value="PDot +3.00 Eur inkl. Versand und 19% MWSt." id="b05" data-parent-option-ids="a00,a130,a131,a132,a133,a134,a138">polka dot rot (E14) 3,00 EUR</option>
  <option value="VK Rot +3.00 Eur inkl. Versand und 19% MWSt." id="b06" data-parent-option-ids="a00,a130,a131,a132,a133">Vichy Karo rot (E14) 3,00 EUR</option>
  <option value="VK Blau +3.00 Eur inkl. Versand und 19% MWSt." id="b07" data-parent-option-ids="a00,a130,a131,a132,a133">Vichy Karo blau (E14) 3,00 EUR</option>
  <option value="taubengraublau +3.00 Eur inkl. Versand und 19% MWSt." id="b08" data-parent-option-ids="a00,a130,a131,a132,a133,a134,a138">taubengraublau (E14) 3,00 EUR</option>
  <option value="nachtblau +3.00 Eur inkl. Versand und 19% MWSt." id="b09" data-parent-option-ids="a00,a130,a131,a132,a133,a134,a135,a136,a138">nachtblau (E14) 3,00 EUR</option>
  <option value="dunkelgr&uuml;n +3.00 Eur inkl. Versand und 19% MWSt." id="b10" data-parent-option-ids="a00,a130,a131,a133,a134,a138">dunkelgr&uuml;n (E14) 3,00 EUR</option>
  <option value="golden +4.00 Eur inkl. Versand und 19% MWSt." id="b11" data-parent-option-ids="a00,a130,a131,a132,a133,a134,a136,a137,a138">gold (E14) 4,00 EUR</option>
  </select>

Thanks for any help, Georg

Georg
  • 41
  • 2

1 Answers1

1

If all your select options have ids then you can simply do this:

document.querySelector('#' + idA).selected = true
document.querySelector('#' + idB).selected = true
dfsq
  • 191,768
  • 25
  • 236
  • 258
  • Thanks. This works but - unfortunatedly in has a conflict with script underneath. Do you have any Idea how to solve it? Firebug says: TypeError: form is null var sel1 = form.Size; index.h...tellung (Zeile 229, Spalte 10) – Georg Aug 23 '16 at 16:45