pufAmuf
pufAmuf

Reputation: 7805

Adding HTML code with Single Quotes inside jQuery

I am trying to add this HTML/JavaScript code into a jQuery variable. I've managed to insert double quotes by writing is with a backshlash \", however the same tactic didn't work for the single quotes:

ajaxData += '<div class=\"menu-item-' + $(this).attr('div') + 'onclick=\"alert('Jquery Function');\"></div>';

Specifically, this part onclick=\"alert('Jquery Function');

Anyone know how I can go around this?

Upvotes: 4

Views: 9912

Answers (4)

Felipe Buccioni
Felipe Buccioni

Reputation: 19668

This is are you trying to do?

$var = "ajaxData += '<div class=\"menu-item-' + \$(this).attr('div') + '" onclick=\"alert(\'Jquery Function\');\"></div>';"

Upvotes: 2

Lenin
Lenin

Reputation: 610

See this, its beautiful:

ajaxData += '<div class="menu-item-' + $(this).attr('div') + ' onclick="alert(\'Jquery Function\');"></div>';

Upvotes: 7

Rohit Choudhary
Rohit Choudhary

Reputation: 2269

ajaxData += '<div class="menu-item-' + $(this).attr('div') + 'onclick="alert('Jquery Function');"></div>';

add escape \ for single quotes. if your string is within single quotes then you can use double quotes without escape but if using single quotes within single quote then you have to insert escape character

Upvotes: 2

Mr. Alien
Mr. Alien

Reputation: 157344

Dirty escape pheeww...Try this

ajaxData += '<div class="menu-item-' + $(this).attr('div') + 'onclick="alert(\'Jquery Function\');"></div>';

Upvotes: 2

Related Questions