Avatar of hankknight
hankknight
Flag for Canada asked on

jQuery: Change html of element added with append()

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

JavaScript

Avatar of undefined
Last Comment
leakim971

8/22/2022 - Mon
maeltar

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
maeltar

Have just changed the page slightly so you can click the button to see the append happen...
ASKER CERTIFIED SOLUTION
leakim971

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck