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

sda
  • 61
  • 6
  • 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);` – Rory McCrossan Jan 10 '18 at 09:38
  • Possible duplicate of [Append to string variable](https://stackoverflow.com/questions/1288095/append-to-string-variable) – Rajesh Jan 10 '18 at 09:42
  • More appropriate duplicate: https://stackoverflow.com/questions/16302941/how-to-concatenate-variable-in-string-in-javascript – Rajesh Jan 10 '18 at 09:44

2 Answers2

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.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • this doesnt work (i added the dot in the beginning). it doesnt append the variable input and it shows in the console. – sda Jan 10 '18 at 09:41
  • I think it's because it has multiple classes – sda Jan 10 '18 at 09:43
  • Please choose to close instead of answering such question. String concatenation is a very basic problem which has answered many times. – Rajesh Jan 10 '18 at 09:44
  • @Rajesh There is another problem with class selector aswell ? – Suresh Atta Jan 10 '18 at 09:44
  • I'm trying to append something to a div with 2 classes. I cant append. – sda Jan 10 '18 at 09:45
  • @ꜱᴜʀᴇꜱʜᴀᴛᴛᴀ OP already has `$('.modal-content')` which means he is aware about selectors and this might be a typo. – Rajesh Jan 10 '18 at 09:47
  • @Rajesh Everything is typo, once you corrected it ;) – Suresh Atta Jan 10 '18 at 09:48
  • 1
    yes it's a typo. I need to append the input to the div with those classes. – sda Jan 10 '18 at 09:49
  • @ꜱᴜʀᴇꜱʜᴀᴛᴛᴀ Nope. A typo is something where a selector on first line is proper and on second line is not. But anyways, lets not invest more time on this discussion. – Rajesh Jan 10 '18 at 09:50
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>
NappingRabbit
  • 1,888
  • 1
  • 13
  • 18