Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2012-03-28
4
Medium Priority
?
161 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:
Gurvinder Pal Singh earned 800 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 1200 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 1200 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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

885 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