Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a string with apostrophe to javascript function

I have a situation where I need to pass a string with an apostrophe in it to a javascript function. This function then takes the string and uses it to find the element by id in the DOM. As an example, I need to call:

showElement('what's')

function showElement(element_id){
     document.getElementById(element_id).style.display = "block";
}

I tried escaping the apostrophe like showElement('what\'s') but that did not seem to work. Is this possible at all?

like image 285
TenJack Avatar asked Feb 27 '23 13:02

TenJack


1 Answers

Have a look at JavaScript Escape Characters

Try using a backslash \

Something like

showElement('what\'s') 

function showElement(element_id){ 
     document.getElementById(element_id).style.display = "block"; 
} 
like image 123
Adriaan Stander Avatar answered Mar 02 '23 12:03

Adriaan Stander