javascript - How to make two select forms of different ids and names return their ids using $(this)? -
i have html form 2 select dropdown lists. each 1 has same sites leave from/go to. i'm trying grab id of each 1 , store them separate variables, each time select new option, variables reset undefined
. code looks this:
$('select[name=depart], select[name=return]').change(function(){ var id = $(this).attr('id'); var depart_id; var return_id; // grabs place left var dep = $('select[name=depart] option:selected').val(); // grabs place traveled var ret = $('select[name=return] option:selected').val(); // grabs day in log string var day_id_raw = $(this).closest('tr').attr('id'); // creates jquery object string above var day_id = $('#' + day_id_raw); // creates substring of string above cutting off @ day number in log. e.g. "day 1" in log becomes number "1". var day_num = day_id_raw.substring(3, day_id_raw.length); //creates jquery object miles of day table column var miles_today = $('#miles_day' + day_num); if($(this).is($('select[name=depart]'))){ depart_id = id; } if($(this).is($('select[name=return]'))){ return_id = id; } // checks if place left contains column output how many miles traveled day. if(day_id.has(miles_today)){ miles_today.text(depart_id + "\n" + return_id); } )};
the last if
statement debugging purposes. miles_today
blank div i'm writing content in order test if variables working. said earlier, each time change option on either select input, alternate variable cleared. in advance.
edit: sorry late reply , ambiguous wording. here working example of have: http://jsfiddle.net/8xvux/2/ enter 1 or 2 in 'number of days' field , should add new row. if click select menu 'departed from' , choose option, it'll output id
in field left, undefined other field 'returned to's id
. want have both id
s displayed when select options changed.
get them outside change function, , use them wherever you'd :
var depart_id = $('select[name=depart]').prop('id'), return_id = $('select[name=return]').prop('id'); $('select[name=depart], select[name=return]').on('change', function(){ miles_today.text(depart_id + "\n" + return_id); });
but sure should'nt getting value ?
Comments
Post a Comment