1

I'm creating a class dinamically.But now when I try to use I cant seem the find a correct way to place the ' and ";

$('.modal-content').append('<div class="modal-body step-' + key + '" data-step="'+key+'"></div>');


$("modal-body step-' + key").append(input);

unexpected token

3
  • You have mismatched quotes in the selector of the second jQuery object. ' should be a ", and the last " should be removed: $("modal-body step-" + key).append(input); Commented Jan 10, 2018 at 9:38
  • Possible duplicate of Append to string variable Commented Jan 10, 2018 at 9:42
  • More appropriate duplicate: stackoverflow.com/questions/16302941/… Commented Jan 10, 2018 at 9:44

2 Answers 2

0

Your concatination is wrong. It should be

$("modal-body step-"+ key).append(input);

No need of single quote at all inbetween. Or if you prefer single quotes, use them completely

$('modal-body step-' + key).append(input);

After you fix that, there is one more issue. Since you are having classes, you have to add . for selector.

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

9 Comments

this doesnt work (i added the dot in the beginning). it doesnt append the variable input and it shows in the console.
I think it's because it has multiple classes
Please choose to close instead of answering such question. String concatenation is a very basic problem which has answered many times.
@Rajesh There is another problem with class selector aswell ?
yes it's a typo. I need to append the input to the div with those classes.
|
0

you can do it like this:

$('.modal-content').append('<div class="modal-body step-' + key + '" data-step="'+key+'"></div>');


$('.step-' + key).append(input);

unless you expect to have multiple divs with step+key. semantically you should use id for the unique part anyway.

edit: ok then do it like this, if you must:

$('.modal-content').append('<div class="modal-body step-' + key + '" data-step="'+key+'"></div>');


$('.modal_body.step-' + key).append(input);

here is a link to tell you why you need the extra . in there.

here is the relevant example from that link:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>class demo</title>
  <style>
  div, span {
    width: 120px;
    height: 40px;
    float: left;
    padding: 10px;
    margin: 10px;
    background-color: #EEEEEE;
  }
  </style>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
 
<div class="myclass">div class="notMe"</div>
<div class="myclass otherclass">div class="myClass"</div>
<span class="myclass otherclass">span class="myClass"</span>
 
<script>
$( ".myclass.otherclass" ).css( "border", "13px solid red" );
</script>
 
</body>
</html>

1 Comment

I have more classes with step+key. it needs to be on the body

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.