Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-26-2009, 01:32 AM   PM User | #1
ffsja
New Coder

 
Join Date: Jul 2005
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
ffsja is an unknown quantity at this point
Question jQuery help?

So I'm using jQuery to add new <SELECT> boxes to my form that get data from a mysql database. It all works great, EXCEPT I cannot get my 'remove' link that I am putting next to my <SELECT> boxes to actually remove the <SELECT> box it's next to. Here's my code:

<script type="text/javascript">
var count = 0;
$(function() {
$('p#add_field').click(function() {
count += 1;
$('#container').append('<select id="field_' + count + '" name="fields[]' + '"> <?php echo javascript_escape($course_block); ?> </select><a href="#" onclick="removeFormField(field_' + count + '); return false;">Remove</a>');
});
});

function removeFormField(id) {
$(id).remove();
}
</script>
__________________
Website:
http://www.taigawebmasters.com/
ffsja is offline   Reply With Quote
Old 11-26-2009, 01:40 PM   PM User | #2
Spudhead
Senior Coder

 
Spudhead's Avatar
 
Join Date: Jun 2002
Location: London, UK
Posts: 1,856
Thanks: 8
Thanked 110 Times in 109 Posts
Spudhead is on a distinguished road
There's a javascript frameworks forum on here that you can ask jquery questions in.

You should take the onclick out of your anchor tag, and handle the click event when you create it. There's probably a better way than this, but this works:

Code:
var count = 0;
$(document).ready(function(){

	$('p#add_field').click(function(){
	
		count++;
		
		$('#container')
		.append(
			$('<select>')
				.attr('id', 'field_' + count)
				.attr('name', 'fields[]')
				.append('<?php echo javascript_escape($course_block); ?>')
		)
		.append(
			$('<a>').addClass('select_remove').attr('href','#').text("Remove")
		)
		
		$('a.select_remove').unbind('click').click(function(){
			$(this).prev('select').remove();
			$(this).remove();
			return false;
		});
	});
});
Spudhead is offline   Reply With Quote
Old 11-30-2009, 07:58 PM   PM User | #3
ffsja
New Coder

 
Join Date: Jul 2005
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
ffsja is an unknown quantity at this point
Thank you, that worked marvelously!
__________________
Website:
http://www.taigawebmasters.com/
ffsja is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:41 AM.


Advertisement
Log in to turn off these ads.