Javascript: Getting variable name from HTML -
i attempting create reusable javascript / jquery function html editable. basically, jquery script class on form select element, , populate select box. it's going use xml document, fed javascript variables.
<form> <select name="topliclist" class="mhs-events" data-event-xml="/xml/events.xml" data-event-text="$fulldate, @ ,$starttime, - ,$location"></select> </form> in example, 'fulldate', 'starttime' , 'location' variables in javascript:
//js var fulldate = 'monday, october 7'; var starttime = '6:30 pm'; var location = 'office building 1'; (don't worry feeding variable xml, can that.)
the 'string' variable equal data-event-text:
//js var string = '$fulldate, @ ,$starttime, - ,$location'; $.each(string.split(","), function(index, item) { //remove $, put specify variable if(item.indexof('$') == 0) { item = $(item.substring(1)); item = item.tostring(); } newstring += item }); $(this).append('<option>' + newstring + '</option>'); instead of 'monday, october 7 @ 6:30 pm - office building 1', populates '[object object] @ [object object] - [object object];
what best way variable name data-event-text attribute , use variable value in javascript?
change
item = $(item.substring(1)); // converts string object item = item.tostring(); to
item = item.substring(1); item = eval(item).tostring(); and dont need add every item newstring return items 1 option.
try:
$.each(string.split(","), function(index, item) { //remove $, put specify variable if(item.indexof('$') == 0) { item = item.substring(1); item = eval(item).tostring(); } newstring += item; }); $('.mhs-events').append('<option>' + newstring + '</option>');
Comments
Post a Comment