Solved

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

Posted on 2012-03-28
4
156 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Only allow one check box 17 39
jQuery Datatables.js Selecting a Row to get ID 5 36
How to escape unsafe HTML tags 4 36
sort Multi-dimensional array 6 11
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 …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

749 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