Solved

jQuery -  execute a function from code that has been appended to a page?

Posted on 2012-03-28
4
158 Views
Last Modified: 2012-03-28
Hi,
Is it possible to execute a function from code that has been appended to a page?

The function updateRecordTotal is executed OK from the original select statement on the page but why is it not executed from the same select statement that has been appended?

Also why is it that appended statements don’t appear when page source in browser is viewed?

Thanks in advance for your feedback.


<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script> 
<script type="text/javascript">
$(document).ready(function(){
  $("button").click(function(){
	$("p").append('<select class="rec_frequency"  id="frequency[33]"><option value="D" >Daily</option><option value="W" >Weekly</option><option value="M" >Monthly</option><option value="M" >Monthly</option></select>');
  });

$(".rec_frequency").change(updateRecordTotal);

});

function updateRecordTotal(){
    alert("updated");
}


</script>
</head>
<body>
<p>
<select class="rec_frequency"  id="frequency[33]"><option value="D" >Daily</option><option value="W" >Weekly</option><option value="M" >Monthly</option><option value="M" >Monthly</option></select>
</p>
<button>Insert Another Select Statement</button>
</body>
</html>

Open in new window

0
Comment
Question by:sabecs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
gurvinder372 earned 200 total points
ID: 37776284
you probably just have to put line 10 just after line 7 and before line 8
0
 
LVL 17

Assisted Solution

by:sonawanekiran
sonawanekiran earned 300 total points
ID: 37776300
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script> 
<script type="text/javascript">
$(document).ready(function(){
  $("button").click(function(){
  $("p").append('<select class="rec_frequency"  id="frequency[33]"><option value="D" >Daily</option><option value="W" >Weekly</option><option value="M" >Monthly</option><option value="M" >Monthly</option></select>');
  });

$(".rec_frequency").live("change",updateRecordTotal);

});

function updateRecordTotal(){
    alert("updated");
}


</script>
</head>
<body>
<p>
<select class="rec_frequency"  id="frequency[33]"><option value="D" >Daily</option><option value="W" >Weekly</option><option value="M" >Monthly</option><option value="M" >Monthly</option></select>
</p>
<button>Insert Another Select Statement</button>
</body>
</html>

Open in new window

0
 
LVL 17

Assisted Solution

by:sonawanekiran
sonawanekiran earned 300 total points
ID: 37776337
You can view appended html if you use firefox firebug
0
 

Author Closing Comment

by:sabecs
ID: 37776355
Thanks for your help..much appreciated.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…
Suggested Courses

626 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