jquery - Error while passing parameter to javascript function dynamically -
jquery - Error while passing parameter to javascript function dynamically -
i creating list dynamically. when user click on li element setcollege method called.
code generate li
is:
$('#dropdown ul').append(" <li onclick=setcollege("+ data[i].id +",'"+ data[i].college_name +"')><i class='fa fa-university'></i>" + data[i].college_name + "</li>");
but javascript dynamically add together "
after space in college name like
<li onclick="setcollege(3,'nirma" university')"> <i class="fa fa-university"></i>nirma university</li>
due "
, produces error while calling js function
onclick html attribute, needs set in quotes itself.
try instead:
var tpl = '<li onclick="setcollege(' + data[i].id + ', ' + data[i].college_name + ' );"><i class="fa fa-university"></i>' + data[i].college_name + '</li>'; $('#dropdown ul').append( tpl );
pay attending single vs. double quote usage.
but since question flagged jquery, i'd suggest:
var listitem = $( '<li></li>' ).text( data[i].college_name ); $( '<i class="fa fa-university"></i>' ).prependto( listitem ); listitem.on( 'click', function() { setcollege( data[i].id, data[i].college_name); }); listitem.appendto( '#dropdown ul' );
javascript jquery html
Comments
Post a Comment