5

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); }); 
3
  • 2
    The best way to answer this kind of question is to try it. What happened when you tried? Commented Sep 26, 2012 at 14:40
  • 1
    content = newContent.add(newContent2); Commented Sep 26, 2012 at 14:41
  • 1
    jsfiddle doesn't bite. Commented Sep 26, 2012 at 14:41

3 Answers 3

6

You can use .add

content = newContent.add( newContent2 ); 
Sign up to request clarification or add additional context in comments.

Comments

5

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); }); 

Comments

0
$("#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>'; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.