views:

34

answers:

4

Hi guys, I have an HTML form consisting of some text-inputs. I'm using jqTransform to prettify them. Now I want to add two buttons that will add and remove text-input dynamically.

Adding is easy: $('#container').append(''); $('input[name=foo]:last-child').jqTransInputText();

But removing is tricky: $('WHAT-TO-SELECT').remove();

I can't do "input[name=foo]:last-child" since it's already wrapped (some divs) by jqTransform. To put this question in another way: How do I select based on "who has child of type input with 'foo' as its name"?

+1  A: 

You might try selecting the input with 'foo' as its name and grabbing it's parent.

$('input[name=foo]').closest('.parentSelector').remove();

Alex Sexton
+1  A: 

Not completely sure what you mean. but it looks like you could be doing more work on it by just chaining.. ie..

$('input[name=foo]:last-child').jqTransInputText().remove();

I'm not sure if that's exactly what you are looking for.. if not, look into the find function in jQuery.

Sorry.. more info on find. Basically, find can either filter your current collection in the chain. ie.. `$("p").find("span").css('color','red');`will only change the colors of the paragraphs with spans contained in them. You could also do this by `$('p span')`, but using find allows you to do things like `$('p').slideUp().find('span').css('color','red');`
+1  A: 
$('input[name=foo]').parent().remove();

Additionally the parent method can be supplied with an normal selector as well to only remove a specific parent.

ntziolis
A: 

I closed the issue not by giving solution to the problem, instead I enclose the input tag with div thus eliminating the problem. So to remove the input tag, I simply remove the div. Thanks anyway guys!

wiradikusuma