Solved

jQuery: Change html of element added with append()

Posted on 2012-03-11
3
184 Views
Last Modified: 2012-03-11
Using jQuery, how can I add text to an element I add using append?

Can this be done with one line?

In the example below, the text is NOT added to the paragraph that is created with append()

<html>
<head>
<title>Hello</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function() {
  $('div').append('<p />').html('This text should in inside the p with a red background');
});
</script>


<style type="text/css">

div {
margin: 50px auto;
padding: 30px;
border: 1px solid #000;
background: #aaaaff;
width: 500px;
}

p {
width: 200px;
background: #ffaaaa;
margin: 50px auto;
padding: 30px;
border: 1px solid red;
}



</style>

</head>
<body>

<div></div>

</body>
</html>

Open in new window

0
Comment
Question by:hankknight
  • 2
3 Comments
 
LVL 11

Expert Comment

by:maeltar
ID: 37706983
Line 8 should read :

$('div').append('<p>This text should in inside the p with a red background</p>');

Open in new window


Example : http://sdparkes.co.uk/ee_ex/jquery_append.php
0
 
LVL 11

Expert Comment

by:maeltar
ID: 37706992
Have just changed the page slightly so you can click the button to see the append happen...
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 37707040
another one :
$('div').append(  $('<p />').html('This text should in inside the p with a red background')  );

Open in new window

or
$('div').append('<p />').children("p:last").html('This text should in inside the p with a red background');
¿

Open in new window


because : $('div').append('<p />')
return a ref on the div and not on the p
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now