jQuery methods $.add() and $.append() operate properly?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/code-flow.club/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
8 views
wrote a small primerchik. it needs to add by clicking on an element . met with jQuery recently and work specifically with the functions of $.add() and $.append() is puzzling. here is a brief code:
.div {background-color: yellow;width: 50pt;height: 50pt;}

$("p #add").click(function() {$(this).add("div").css("class", "div");});$("p #append").click(function() {$(this).append("");});

click me and I'll try to show you a div with $.add()click me and I'll try to show you a div with $.append()

links API:
$.append()
$.add()

I have clicking in both cases, nothing happens. wtf?

fully download and open test.html in browser here

UPD: jQuery code now:
$(document).ready(function(){ $("p#add").click(function() { $(this).add("div").css("class", "div"); }); $("p#append").click(function() { $(this).append(""); }); }); 

$.append() earned $.add() — is still there
by | 8 views

5 Answers

0 like 0 dislike
As I understand it, You want to find a analog call
$(function(){ $("p#append").click(function(){ $(this).append(""); }); }); 
which would create an element by name and then supplied him with the class, and then attached to the specified one.
\r
Then it is this:
$(function(){ $("p#append").click(function(){ $('').addClass('div').appendTo($(this)); }); });

Method .add() will not work because it is used to replenish $-sets. For example, it can be used to add two div-elements instead of one:
$(function(){ $("p#append").click(function(){ $('').add('').addClass('div').appendTo($(this)); }); });

Method .css() will not work because it is used to overlay the CSS properties. For example, this call:
$(function(){ $("p#append").css('color', 'red'); });
imposes a property
{color: 'red';}
by
0 like 0 dislike
1)
\rSyntax error at line 5 while loading:
$("p #append").click
^
expected ')', got '$'

\r
2)
\r$("p #add") → $("p#add")
$("p #append") → $("p#append")
by
0 like 0 dislike
$.append() earned $.add() — is still there

What does not work? Let us examine your line:
\r
$(this).add("div").css("class", "div");
\r
1) Select in the object element this (paragraph).
2) Add this object with all paragraphs found on the page of the diva. As DIVS on the page, probably not, does not change the result, $(this).
3) Applied to these objects .css("class", "div").
\r
By the way, what is this new css-property, class? Never even heard of.
\r
With regard to paragraph 1) Syntax error at line 5 while loading:
The proposed download the archive code looks like this:
\r$("p #add").click(function() {...}
$("p #append").click(function() {...}

Without closing braces and semicolons.
by
0 like 0 dislike
About the append method can not say anything, but that's about jQuery add sufficient detail.
by
0 like 0 dislike
guys, thanks for all the answers, but the question was asked 4 years ago. it has long been irrelevant.
by

Related questions

0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
110,608 questions
257,186 answers
0 comments
22,133 users