wcdomy
wcdomy

Reputation: 215

How can you concatenate two variables in jquery?

Can I do the following to concatenate two variables into one? Or there is a better way to do it?

   $('.row-add').live("click", function () {
          var newContent = $("<span>Example data</span>"+"");
          var newContent2 = $("<span>New Project</span>");
          var content = newContent+newContent2;
          $(this).closest("td").append(content);
        });

Upvotes: 5

Views: 25914

Answers (3)

Aslam Khan
Aslam Khan

Reputation: 378

$("#submit").click(function(){
       
              var name = $('#name').val();
              var note = $('#note').val();
              
              var data = '<h3>' + name + '</h3>' + '<p>' + note + '</p>'; 
                console.log(data); 
          });
 <script 
  src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
 
 <form role="form" method="POST" id="reused_form">
                        <div class="row">
                        </div>
                         <div class="row">
                            <div class="col-sm-6 form-group">
                                <label for="name"> Your Name:</label>
                                <input type="text" class="form-control inputVal" value="" id="name" name="name" required>
                            </div>

                        <div class="row">
                            <div class="col-sm-12 form-group">
                                <label for="comments"> Note:</label>
                                <textarea class="form-control inputVal" type="textarea" name="note" id="note" placeholder="Write Somthing" maxlength="6000" rows="7" value=""></textarea>
                            </div>
                        </div>
                       
                        <div class="row">
                            <div class="col-sm-12 form-group">
                                <button type="submit" class="btn btn-lg btn-warning btn-block" name="submit" id="submit">Submit </button>
                                 <button type="button" class="btn btn-lg btn-warning btn-block" name="reset" id="reset" onclick="clearLocalStorage()">Reset </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>

Try this

var name = $('#name').val();
var note = $('#note').val();
var data = '<h3>' + name + '</h3>' + '<p>' + note + '</p>'; 

Upvotes: 0

moonwave99
moonwave99

Reputation: 22817

If you use + between jQuery objects, you'll get the concatenation of String representations [fiddle].

If you want to append multiple elements, just do it.

$('.row-add').live("click", function () {
       var newContent = $("<span>Example data</span>"+"");
       var newContent2 = $("<span>New Project</span>");
       $(this).closest("td").append(newContent).append(newContent2);
});

Upvotes: 5

aziz punjani
aziz punjani

Reputation: 25766

You can use .add

content = newContent.add( newContent2 ); 

Upvotes: 6

Related Questions