Like this:
var str = 'blah blah blah';
str += ' blah';
str += ' ' + 'and some more blah';
Answer from karim79 on Stack Overflow[jQuery] .append() variable content rather than text
How to append to a variable in jquery?
Jquery - appending html string to html string in variable
Jquery - appending a string from an array to a div
This $('#golden_site') selects the div with id=golden_site. While this $(golden_site) doesn't select anything.
Taken from here, you have the following ways of selecting an element using jQuery
- Selecting Elements by ID
- Selecting Elements by Class Name
- Selecting Elements by Attribute
- Selecting Elements by Compound CSS Selector
- Pseudo-Selectors
The way you tried to select your div doesn't follow one of the above ways. Hence you didn't make it. While using the id you made it, since this is included in the above ways.
update
As Guffa pointed out (I didn't now it) in his comment,
The call $(golden_site) doesn't try to use the string as a selector at all. It will create an elements from the HTML string, and actually return that element
The code is working fine, but it doesn't do what you think.
The $(golden_site) part will create a new div element from the HTML code in the string. The lookup element will then be appended to that div. As the div is an element that you just created, it's not in the page and the lookup element that you appended to it isn't in the page either.
If you create the div element first and then append that to the page, instead of using a string in the append, then you have a reference to the div element:
var golden_site = '<div id="golden_site"></div>';
var element = $(golden_site);
$('.form_content').append(element);
Now you can append things to it:
element.append(lookup);
Updated fiddle.
You have to assign the return of this expression $(thing).append(close); to the variable thing like:
thing = $(thing).append(close);
Else the variable will always hold the default string <div class="thing"></div> as value.
Hope this helps.
$(document).ready(function(){
$('body').on('click', 'button', function(){
var thing = '<div class="thing"></div>';
var close = '<a href="#" class="close">close</a>';
$('.canvas').append( $(thing).append(close) );
return false;
});
});
.thing {
width: 50px;
height: 50px;
background: red;
}
.close {
background: blue;
color: white;
}
.canvas {
border: 1px solid black;
width: 500px;
height: 500px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>Add Thing</button>
<div class="canvas"></div>
The append method of jquery append to DOM of the page. If you want to make more readble try this:
var thing = '<div class="thing">';
thing += '<a href="#" class="close">close</a>';
thing += '</div>';
$('.canvas').append(thing);
You can do normal string concatenation instead of using any library (like use used .append() )
$(document).ready(function() {
$(document).on('click', '.button', function() {
var demo = $('#textbox').val();
var demoapp = demo + "^";
alert(demoapp);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<body>
<input type="textbox" id="textbox" />
<input type="submit" class="button" value="submit" />
</body>
</html>
Instead of var demoapp=demo.append("^");, use var demoapp=demo+"^";
Your var simple should be a string, without the $.
var simple = "<p>hello</p>";
$(".easy").append(simple);
var simple = '<p> hello </p>';
$('.easy').append(simple);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="easy"> </div>
If you want to create HTML template, you can also use backticks.
var atLeastOneIsChecked = jQuery('input[name="addon-"'+abc+']:checked').length;
^
|
You used the closing " at the wrong place
var atLeastOneIsChecked = jQuery('input[name="addon-'+abc+'"]:checked').length;
^
|
Try this -
var atLeastOneIsChecked = jQuery("input[name='addon-"+abc+"']:checked").length ;
Concatenating can be done with + as follows.
$(".html").html('<div class="new" id="' + id + '">jitender</div>");
or in the modern world you can use template strings:
$(".html").html(`<div class="new" id="${id}">jitender</div>`);
The more jQuery-oriented approach would be:
var id = "some-id";
$("<div>", {
"class": "new",
id: id,
text: "jitender"
}).appendTo(".html");
.new {
background: #f0f;
}
#some-id {
color: #fff;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="html"></div>
References:
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#string_operators
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
You can also use template literals:
$('.html').html(`<div class='new' id=${id}>jitender</div>`)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
var $wrap = $('<div class="wrap"><div data-role="controlgroup"></div></div>');
var $btn = $('<a href="#some" data-role="button">Click</a>');
$wrap.append( $btn );
There's probably fifty ways to this, like:
var $wrap = $('<div class="wrap"><div data-role="controlgroup"></div></div>'),
$btn = '<a href="#some" data-role="button">Click</a>';
$wrap.children().append( $btn );
or:
var $wrap = $('<div class="wrap"><div data-role="controlgroup"></div></div>'),
$btn = '<a href="#some" data-role="button">Click</a>';
$('[data-role="controlgroup"]', $wrap).append( $btn );
You simply have strings, you need a jquery object to call append:
var $wrap = $('<div class="wrap"><div data-role="controlgroup"></div></div>');