Save of escaped quote inside of javascript variable containing string that's nested html (jelly)

Brian Whyte
Kilo Guru

Help, how do successfully put an attribute inside a HTML tag inside a string without jelly compiler complaining and preventing me from saving? This is what it should be syntactically when it's just javascript and not put inside jelly:

 

<script>
//Get some dynamic value via GlideRecord
//.... you get the idea...

//Write the dynamic HTML using this value
var dyn_html = "<table><tr><td colspan=\'2\'>" + dyn_value + "</td></tr></table>";

//Write the content 
document.getElementById("dyn_content").innerHTML=dyn_html;
</script>

 

Error thrown when save is: Open quote is expected for attribute "colspan" associated with an element type "td". 

 

The Service Now UI complains about saving it until I adjust it to this where the quotes are NOT escaped which makes the code not work. So, this allows me to save, but at that point, it doesn't work because the single quotes need to be escaped and I get a console error when running client side:

 

<script>
//Get some dynamic value via GlideRecord
//.... you get the idea...

//Write the dynamic HTML using this value
var dyn_html = "<table><tr><td colspan='2'>" + dyn_value + "</td></tr></table>";

//Update the content 
document.getElementById("dyn_content").innerHTML=dyn_html;
</script>

 

I'm sure there's some weird way to escape this for jelly or tell it to stop validating an attribute within a string that should not be validated because it's technically a string and not code, but I don't have a clue.  

1 ACCEPTED SOLUTION

Gurpreet07
Mega Sage

Below code works

 

<script>
//Get some dynamic value via GlideRecord
//.... you get the idea...

//Write the dynamic HTML using this value
var dyn_html = '<table><tr><td colspan="2">+ dyn_value + '</td></tr></table>';

//Write the content
document.getElementById("dyn_content").innerHTML=dyn_html;
</script>

View solution in original post

2 REPLIES 2

Gurpreet07
Mega Sage

Below code works

 

<script>
//Get some dynamic value via GlideRecord
//.... you get the idea...

//Write the dynamic HTML using this value
var dyn_html = '<table><tr><td colspan="2">+ dyn_value + '</td></tr></table>';

//Write the content
document.getElementById("dyn_content").innerHTML=dyn_html;
</script>

Brian Whyte
Kilo Guru

Indeed it does. Thanks @Gurpreet07 . The only slight change, but we all get the idea (was missing one single quote after the "2">.  

 

<script>
//Get some dynamic value via GlideRecord
//.... you get the idea...

//Write the dynamic HTML using this value
var dyn_html = '<table><tr><td colspan="2">'+ dyn_value + '</td></tr></table>';

//Write the content
document.getElementById("dyn_content").innerHTML=dyn_html;
</script>