Quotes in javascript string +variable

331
February 11, 2017, at 12:48 PM

I can't make it work, btn is a string here but 'licz' is a variable, I've tried anything, could someone help me and tell what is wrong with that ? Licz is a counter from 1 to 100, i want that line to give something like

btn1, btn2 etc

    $('#slider').append("<div id='btn ' + \'licz><i class='fa fa-circle fa-2x' aria-hidden='true'></i></div>");

Can someone show me the proper way to do this ? I would be gratefull

Answer 1
    $('#slider').append("<div id='btn" + licz + "'><i class='fa fa-circle fa-2x' aria-hidden='true'></i></div>");
Answer 2

You would need to append it to your string like so:

 $('#slider').append("<div id='btn" + licz + "'><i class='fa fa-circle fa-2x' aria-hidden='true'></i></div>");

Or if you are using ES6

$('#slider').append(`<div id='btn${licz}'><i class='fa fa-circle fa-2x' aria-hidden='true'></i></div>`);
Answer 3

You need to stop and start the string appropriately where you want to. So you might instead like to try

$('#slider').append("<div id='btn " + licz + "><i class='fa fa-circle fa-2x' aria-hidden='true'></i></div>");

Breaking the string properly with double quotes to allow you to insert the variable.

Answer 4

Your variable is not appended correctly it needs to be like this:

$('#test').append("<div id='btn '>" + licz + " <i class='fa fa-circle fa-2x' aria-hidden='true'></i></div>");

HERE is an example of it working

READ ALSO
JSON.parse() for very large JSON files (client side)

JSON.parse() for very large JSON files (client side)

Let's say I'm doing an AJAX call to get some JSON data and it returns a 300MB+ JSON stringAfter it finishes downloading, I need to parse it

346
Structure blog posts by using post ids as id attribute

Structure blog posts by using post ids as id attribute

How do I structure blog posts/comments in HTML so that I can select them later on using jQuery/Javascript for updating/deleting/Ajax calls?

296
Javascript transition works only after second hover

Javascript transition works only after second hover

I am new to JavascriptI want to make my image gradually get smaller, and then resize back to its original size

304