Converting text fields to an array and displaying on page with Javascript/Jquery -


i converting collection of text fields array using following code (assume html input fields values):

var bgimgsarray = $("input[class='image_name_input']").map{function(){return $(this).val();}).get(); 

then display on page using:

$("#vardisplay").html("var bgimgs = ["+bgimgsarray+"];"); 

that displays as:

var bgimgs=[image1.jpg,image2.jpg,image3.jpg]; 

however, need display this:

var bgimgs=['image1.jpg','image2.jpg','image3.jpg']; 

other informing user put ' @ beginning , end of input, how achieve this? feel should simple driving me crazy.

change way mapping value.

from

return $(this).val(); 

to:

return "'"+$(this).val()+"'"; 

ending code:

var bgimgsarray = $("input[class='image_name_input']").map{function(){return "'"+$(this).val()+"'";}).get(); 

you may want make sure don't add 's when user types them. in case, make it:

return "'"+$(this).val().replace(/^'|'$/g,'')+"'"; 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -