Using quotation marks in Javascript with innerHTML symbols apostrophe

Using quotation marks in Javascript with innerHTML symbols apostrophe

The above code is my javascript. Problem is printing hello or any other string. If I just type 123 in place of hello, it does give alert. But am not able to use a string like hello there. Normally a string in an alert function is kept inside quotes ‘ ‘ but the entire content is inside double quotes and I have already used single quote at the beginning of onclick function. I tried using Escape character (“\”) but it didnt help. Any suggestions?

Using quotation marks in Javascript with innerHTML symbols apostrophe
Using quotation marks in Javascript with innerHTML symbols apostrophe

HTML code:

<label id=”changeText”><input onchange=”addSelectedOption(‘text321′,this.checked);” type=”checkbox” name=”mistakeQuestionFlg” value=”true”>Text321</label>

I prefer using apostrophes as string delimited in Javascript and quotation marks as delimiters for HTML attributes. Then you just escape the apostrophes that you have inside the Javascript string:

$(‘#abcID’).on(‘click’, function (e) {

   var labelContent = “<input onchange=\”addSelectedOption(\’text123\’,this.checked);\” type=\”checkbox\” name=\”mistakeQuestionFlg\” value=\”true\”>Text123″;

   document.getElementById(‘changeText’).innerHTML = labelContent;

}

Using quotation marks in Javascript with innerHTML symbols apostrophe
Using quotation marks and replace text in Javascript with innerHTML symbols apostrophe

Trả lời

Thư điện tử của bạn sẽ không được hiện thị công khai. Các trường bắt buộc được đánh dấu *

Bạn có thể sử dụng các thẻ HTML và thuộc tính sau: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>