Pull value from <select> with Javascript and loop -


using javascript (no jquery) project need pull value select box , compare predefined value. if match, show message box user , change select dropdown shows value compared

function displayrow(preevent){  if ((preevent != 'null') && (preevent !== 0)) {  //var mystring = ""; //var splitstring = mystring.split(':');  var splitstring = preevent.split(':');  for(var i, j = 0; = document.getelementbyid('thursevent').getelementsbytagname("option")[j]; j++) {   var sel = document.getelementbyid('thursevent').value;   var splitsel = sel.split(':');    if(splitsel[3] == splitstring[3]) {     document.getelementbyid("thursevent").selectedindex = j;     break;   } }}   <select name="thursevent" id="thursevent" onchange="displayrow(this.value);" >   <option value="0"> -- please select here -- </option>   <option value="645:5:227:640">t00</option>   <option value="563:0:84:654">t01am</option>   <option value="564:0:116:563">t01pm</option>   <option value="565:0:176:640">t02am</option>   <option value="566:0:1:640">t02pm</option>   <option value="567:0:24:640">t03</option>   <option value="568:0:154:640">t04</option>   <option value="569:0:108:0">t05</option>   <option value="570:0:651:0">t06</option>   <option value="571:0:201:0">t07</option> </select> 

--> updated final example found here: sample

fiddle

i believe understand want. data in format of coursenumber:something:something:prerequisite. when user selects item, need check if has prerequisite, , change select element prerequisite (plus alert).

function displayrow(preevent) {     if (preevent == null || parseint(preevent) === 0) return; 

first, should clean our variables. makes code confusing.

    var selectelement = document.getelementbyid('thursevent');     var optionelements = selectelement.getelementsbytagname("option");     var prerequisite = preevent.split(':')[3]; 

we can save work if prerequisite 0, because we'll never have course 0.

    if (parseint(prerequisite) === 0) return; 

i changed variable names here, biggest change we're comparing our prerequisite course number of each option. note we're checking index 0 instead of 3, in previous code. believe error.

    (var opt, = 0; opt = optionelements[i]; i++) {         var coursenumber = opt.value.split(':')[0];         if (prerequisite == coursenumber) {             alert("the course selected has pre-requested course.  must add registration first.");             selectelement.selectedindex = i;     } } 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -