Answer the question
In order to leave comments, you need to log in
jQuery: $.add() and $.append() methods not working correctly?
wrote a small example. it should add an element to it by clicking on it. I got acquainted with jQuery recently and the work of these $.add() and $.append() functions is puzzling. here is the code in short:
<style><br>
.div {<br>
background-color: yellow;<br>
width: 50pt;<br>
height: 50pt;<br>
}<br>
</style><br>
<script type="text/javascript"><br>
$("p #add").click(function() {<br>
$(this).add("div").css("class", "div");<br>
});<br>
$("p #append").click(function() {<br>
$(this).append("<div class='div'></div>");<br>
});<br>
</script><br>
<p id="add">click me and i'll try to show you a div with $.add()</p><br>
<p id="append">click me and i'll try to show you a div with $.append()</p><br>
$(document).ready(function(){<br>
$("p#add").click(function() {<br>
$(this).add("div").css("class", "div");<br>
});<br>
$("p#append").click(function() {<br>
$(this).append("<div class='div'></div>");<br>
});<br>
});<br>
Answer the question
In order to leave comments, you need to log in
As I understand it, you want to find such an analogue of the call
$(function(){
$("p#append").click(function(){
$(this).append("<div class='div'></div>");
});
});
which would create an element by name and then provide it with a class, and then attach it to the specified one. $(function(){
$("p#append").click(function(){
$('<div />').addClass('div').appendTo($(this));
});
});
$(function(){
$("p#append").click(function(){
$('<div />').add('<div />').addClass('div').appendTo($(this));
});
});
$(function(){
$("p#append").css('color', 'red');
});
imposes a property{color: 'red';}
1)
2)
Syntax error at line 5 while loading:
$("p #append").click
^
expected ')', got '$'
$("p #add") → $("p#add")
$("p #append") → $("p#append")
$.append() worked, $.add() still didn't
$("p #add").click(function() {…}
$("p #append").click(function() {…}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question