jquery - wrong html output from the javascript -


i wrote script populating selectbox bunch of options.

initially data in form of string of format "key=value;key2=value2;etc...":

//split string distinguish between different options populate selectbox var values = data.split(';'); //reset length of selectbox populated document.getelementbyid(child).options.length = 0; //create first default option document.getelementbyid(child).options[0] = new option('all', '0'); for(var = 0; < values.length; i++){     //check , remove unnecessary characters     values[i].replace(/\s+/g, '');     //split option key , value separately     var options = values[i].split('=');     if(!isempty(options[0]) && !isempty(options[1])){         //insert new element selectbox         document.getelementbyid(child).options[i+1] = new option(options[1], options[0]);     } } 

the example above populates selectbox given html output:

<option value="0">all</option> <option value="  7">bermuda</option> <option value="10">british virgin islands</option> <option value="15">cayman islands</option> <option value="42">jamaica</option> <option value="74">st. lucia</option> <option value="79">trinidad tobago</option> 

as can notice above second option in selectbox has corrupted string value. need fix value because because of cake cannot save value properly.

if have other questions please ask.

you should try trim values:

document.getelementbyid(child).options[i+1] = new option(    options[1].replace(/^\s+|\s+$/g, ''),     options[0].replace(/^\s+|\s+$/g, '') ); 

or if using jquery:

document.getelementbyid(child).options[i+1] = new option(    $.trim(options[1]),     $.trim(options[0]) ); 

also should close on fragment:

values[i].replace(/\s+/g, ''); 

because doesn't want. first, removes whitespaces string "new york city" become "newyorkcity". next thing replace method returns new string code take no effect. should be:

values[i] = values[i].replace(/\s+/g, ''); 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -