0

I am creating list dynamically. When user click on that li element setCollege method will be called.

Code to 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 " after space in college name like

<li onclick="setCollege(3,'Nirma" university')"> <i class="fa fa-university"></i>Nirma University</li>

due to ", it produces error while calling js function

5
  • Is Nirma" university the name? Commented Jun 19, 2014 at 9:25
  • 1
    escape(data[i].college_name) Commented Jun 19, 2014 at 9:27
  • 1
    escape does URL encoding, not HTML encoding. It's the obsolete name of encodeURIComponent. Commented Jun 19, 2014 at 9:28
  • @Barmar, ok encodeURIComponent(data[i].college_name) then inside setCollege() the OP can just use decodeURIComponent() Commented Jun 19, 2014 at 9:36
  • 1
    @Barmar and Reigel : No need to escape the data[i].college_name. It doesn't have any double quotes in between. See the resulted output value of data[i].college_name was <li ......><i class="fa fa-university"></i>Nirma University</li><li>. Commented Jun 19, 2014 at 9:39

4 Answers 4

1

onclick is a html attribute, thus it needs to be put in quotes itself.

Try this 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 attention to single vs. double quote usage.

But since your question is flagged as 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' );
Sign up to request clarification or add additional context in comments.

1 Comment

.text() will replace <i class="fa fa-university"></i>
0

Try using like:

$('#dropDown ul').append("
<li onclick=setCollege("+ data[i].id +",&quot;"+ data[i].college_name +"&quot;)><i class='fa fa-university'></i>" + data[i].college_name + "</li>");

Comments

0

Your concatenation starts with double quotes("). So you need to follow till the end of the statement.

Try this,

"'+ data[i].college_name +'"

instead of

'"+ data[i].college_name +"'

Also add double quotes(" ") surround by the onclick event, and escape them,

onclick=\"setcollege(.......)\"

Comments

0

Because you don't add the surrounding " to your onclick attribute:

$('#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 why don't you use jQuery to attach the event to the list items? This would be the better solution:

$('#dropDown').on('click', 'li', function() {
    setCollege(...);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.