javascript - jQuery undefined value of an existing html element -


i have jquery code check whether html element particular id exists or not. have written if , else clause of existing , non existing html element. when element exists why jquery returns value undefined ?

you can see jquery code below. variable start_time defined, because enters clause condition value detected undefined. wrong code?

  var count = 0;         for(i=1; i<=n_day; i++){              count = count + 1 ;              var start_time = jquery("#txtstarttime_detail"+i);             var end_time = jquery("#txtendtime_detail"+i);             var start_location = jquery("#txtstartlocation_detail"+i);             var end_location = jquery("#txtendlocation_detail"+i);             var start_location_coor = jquery('#txtstartlocation_coordinates_detail'+i);             var end_location_coor = jquery('#txtendlocation_coordinates_detail'+i);             //var day = jquery('#txtday'+i);              if(typeof start_time ==  'undefined'){                 start_time = '';             }else{                 start_time = jquery("#txtstarttime_detail"+i).val();                 alert(start_time);             }             if(typeof end_time ==  'undefined'){                 end_time = '';             }else{                 end_time = jquery("#txtendtime_detail"+i).val();             }             if(typeof start_location ==  'undefined'){                 start_location = '';             }else{                 start_location = jquery("#txtstartlocation_detail"+i).val();             }             if(typeof end_location ==  'undefined'){                 end_location = '';             }else{                 end_location =  jquery("#txtendlocation_detail"+i).val();             }             if(typeof start_location_coor ==  'undefined'){                 start_location_coor = '';             }else{                 start_location_coor = jquery('#txtstartlocation_coordinates_detail'+i).val();             }             if(typeof end_location_coor ==  'undefined'){                 end_location_coor = '';             }else{                 end_location_coor = jquery('#txtendlocation_coordinates_detail'+i).val();             }                  requestdetail += '<div class="yellow" id="txtday'+count+'">day - '+count+' : '+travel_date+'</div>'                                 +'<div class="table-responsive table-condensed">'                                     +'<table class="table borderless">'                                         +'<tbody>'                                             +'<tr>'                                                 +'<td class="col-left">travel time</td>'                                                 +'<td class="col-middle"><input type="text" name="txtstarttime_detail[]" id="txtstarttime_detail"'+count+'" class="timepicker" value="'+start_time+'"/></td>'                                                 +'<td class="col-middle"><input type="text" name="txtendtime_detail[]" id="txtendtime_detail'+count+'" class="timepicker" value="'+end_time+'" /></td>'                                                 +'<td class="col-right"><div class="error" id="error_time'+count+'">&nbsp;</div></td>'                                             +'</tr>'                                             +'<tr>'                                                     +'<td class="col-left">location</td>'                                                 +'<td class="col-middle-2"><input type="text" size="100" name="txtstartlocation_detail[]" id="txtstartlocation_detail'+count+'" class="inputwithimge" value="'+start_location+'" onmouseover="display_text(this)" />'                                                 +   '<img src="'+base_url+'" class="location-icon" alt="click search location" name="location-icon" value="startlocation_detail'+count+'" title="click show map"/>'                                                    + '</td>'                                                 +'<td class="col-middle-2"><input type="text" name="txtendlocation_detail[]" id="txtendlocation_detail'+count+'" class="inputwithimge" value="'+end_location+'" onmouseover="display_text(this)"/>'                                                 +   '<img src="'+base_url+'" class="location-icon" alt="click search location" name="location-icon" value="endlocation_detail'+count+'" title="click show map"/>'                                                      + '</td>'                                                 +'<td class="col-right"><div class="error" id="error_location'+count+'">&nbsp;</div></td>'                                             +'</tr>'                                         +'</tbody>'                                         +'<input type="hidden" name="txtstartlocation_coordinates_detail[]" id="txtstartlocation_coordinates_detail'+count+'" value="'+start_location_coor+'">'                                         +'<input type="hidden" name="txtendlocation_coordinates_detail[]" id="txtendlocation_coordinates_detail'+count+'" value="'+end_location_coor+'">'                                     +'</table>'                                 +'</div>';              travel_date = new date(jquery("#txtstartdate").val());             travel_date.setdate(startdate.getdate() + i);             travel_date = jquery.datepicker.formatdate("dd, mm d, yy", new date(travel_date));       } 

the html elements populated dynamically in codeigniter , returns browsers html string.

here how display html string :

<form name="frm_rrequest" action="#" method="post">                     <?php                          if(strlen($current_request)>0 || $current_request!=null){                             echo $current_request;                             echo '<div><input type="button" name="btn_update" class="button" value="update" /></div>';                         }else{                             $site_url = site_url("user/recommendation_request");                             echo '<div class="yellow">you have no current request, can make new request here >> <a href="'.$site_url.'">recommendation request</a></div>';                         }                     ?>                 </form>  

and here how elements created in codeigniter

'<div class="yellow" id="txtday'.$count.'">day - '.$count.' : '.$travel_date.'</div>'                             .'<div class="table-responsive table-condensed">'                                 .'<table class="table borderless">'                                     .'<tbody>'                                         .'<tr>'                                             .'<td class="col-left">travel time</td>'                                             .'<td class="col-middle"><input type="text" name="txtstarttime_detail[]" id="txtstarttime_detail"'.$count.'" class="timepicker" value="'.$start_time.'"/></td>'                                             .'<td class="col-middle"><input type="text" name="txtendtime_detail[]" id="txtendtime_detail'.$count.'" class="timepicker" value="'.$end_time.'" /></td>'                                             .'<td class="col-right"><div class="error" id="error_time'.$count.'">&nbsp;</div></td>'                                         .'</tr>'                                         .'<tr>'                                                 .'<td class="col-left">location</td>'                                             .'<td class="col-middle-2"><input type="text" size="100" name="txtstartlocation_detail[]" id="txtstartlocation_detail'.$count.'" class="inputwithimge" value="'.$row->start_location.'" onmouseover="display_text(this)" />'                                             .   '<img src="'.$base_url.'" class="location-icon" alt="click search location" name="location-icon" value="startlocation_detail'.$count.'" title="click show map"/>'                                              . '</td>'                                             .'<td class="col-middle-2"><input type="text" name="txtendlocation_detail[]" id="txtendlocation_detail'.$count.'" class="inputwithimge" value="'.$row->end_location.'" onmouseover="display_text(this)"/>'                                             .   '<img src="'.$base_url.'" class="location-icon" alt="click search location" name="location-icon" value="endlocation_detail'.$count.'" title="click show map"/>'                                                    . '</td>'                                             .'<td class="col-right"><div class="error" id="error_location'.$count.'">&nbsp;</div></td>'                                         .'</tr>'                                     .'</tbody>'                                     .'<input type="hidden" name="txtstartlocation_coordinates_detail[]" id="txtstartlocation_coordinates_detail'.$count.'" value="'.$start_location_coor.'">'                                     .'<input type="hidden" name="txtendlocation_coordinates_detail[]" id="txtendlocation_coordinates_detail'.$count.'" value="'.$end_location_coor.'">'                                 .'</table>'                             .'</div>'; 

your problem in strings use create content.

you have

... id="txtstarttime_detail"'+count+'"..... 

which create id

...id = "txtstarttime_detail"1"... 

you have remove double quotes "'+count"' should '+count+'"


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -