Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

760 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

19 Experts available now in Live!

Get 1:1 Help Now