javascript - How to nest required apostrophe in onclick with double quotes -


so have onclick function this

onclick= "js.popup('js.name', 'db.name', 'text')" 

in context db stands database,js.name, , text text comes back. db.name variable that's determined on comes back.

the problem db.name result apostrophe pulled database , function throws error. function supposed launch , open specific item user. works every user, throws error names apostrophes.

so have made research , came this:

html

<div id="wrapper">  </div> 

javascript

var js = {    checkstring: function(s) {     if (s.indexof("'") > -1) {         s = s.replace(/'/g , "&#39"); //html escapes code apostrophe     }      return s;   },    appendhtml: function(s) {     var cleanstring = js.checkstring(s);     $("#wrapper").append("<div onclick = 'js.popup(\"" + cleanstring + "\")'>click me</div>");   },     popup: function (message) {     alert(message);   } }  js.appendhtml("stringwith'apostrophe"); 

here working jsfiddle


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -