How to concatenate variable in a string in jQuery?


You can easily concatenate variable in a string in jQuery, using the jQuery html() method. Try to run the following code to learn how to use variable in a string with jQuery −

Example

Live Demo

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
    $(function(){
      $("a").click(function(){
        var id= $(".id").html();
        $('.demo').html("<br><div class='new' id='" + id + "'>Welcome to </div>");            
      });
    });
    </script>
</head>
<body>
   <div class="wrap">
      <a href="#">Click me</a>
      <div class="demo"></div>
      <div class="id">Qries</div>
   </div>
</body>
</html>

Updated on: 13-Feb-2020

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements