Exclude single quotes in jQuery or JavaScript

I want to display the following text for the tag <span>. How to avoid single quotes for the following?

$("#spn_err").text($('#txt1').attr('value')+" is not valid");

I want to display the message as' ZZZ' is not valid. Here $('#txt1').attr('value')is the dynamic meaning. It can be abc, bcc, ddd, zzz. How can i do this?

+5
source share
4 answers

Like this:

$("#spn_err").text("'" + $('#txt1').val() + "' is not valid");

Inside double quotes, single quotes are normal characters and vice versa. Otherwise, you can avoid them by adding a backslash: "\""or '\''.

+11
source
$("#spn_err").text('\'' + $("#txt1").attr("value") + '\' is not valid');
+5
source

. .val():

$('#spn_err').text($('#txt1').val() + ' is not valid');
+1
source
$("#spn_err").text("'" + $('#txt1').attr('value') + "' is not valid");
0
source

All Articles