Solved

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

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

Independent Software Vendors: 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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

710 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