0

I have p element like this :

<p class="11"></p>

I try to change the content by append() method through clear.js like this :

$('.11').append("taylor");

it works fine and print "taylor" on my page, but if I change my code like this :

number=11;
classProv = "'." + number + "'";
$(classProv).append("taylor");

it returns error message :Syntax error, unrecognized expression: '.11'

Why did it happen ? Any solution?

3 Answers 3

2

The problem is that you're including some extra quotation marks. Drop the extra quotes and it works just fine.

number=11;
classProv = "." + number;
$(classProv).append("taylor");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="11"></p>

Sign up to request clarification or add additional context in comments.

Comments

2

Remember.

If you write

 $('.11').append("taylor");

You provide a string as parameter explicitly. By declaring a variable like var classProv = '.' + number; and using it as parameter you provide a string. So there is no additional quotes needed.

Comments

0

The string you are concatenating ends up literally being "'.11'", which contains single quotes in the value.

What you need is:

var number = 11;
var classProv = "." + number;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.