Skip to content Skip to sidebar Skip to footer

Adding A Text Input Field Through Js Or JQuery When Clicking On Another Input Field In Rails 3

I'm building a nested form in rails 3.1 and instead of pressing the 'add' button i want to automaticly add an input field when typing text in an empty input field (just like making

Solution 1:

it's really easy. Just tap into the focus() event handler and add textboxes as you go along.

I have created a simple demo for you here: http://jsfiddle.net/mcVnN/

Warning: clicking any textbox will add a new textbox here. If you want to add new textbox ONLY on last textbox focus, just compare the id.

If you have any doubts let me know.

Cheers

-- Sorry, did not read your full question, my mistake. I have put together some quick code http://jsfiddle.net/mcVnN/12/ again on jsfiddle, but you might want to change it to suit your needs.


Solution 2:

I was going through my answers a now and realized I had left you in the middle of nowhere.

Took some time to completely re-write my answer - the above answer is a lot buggier, you are better off using the :last selector than manually trying to keep track of the last text box yourself.

See my rewrite here: http://jsfiddle.net/mcVnN/29/

I have also added a max text box count that you can change easily to configure how many textbox you want users to be able to add.

Please replace the above code with this one, if you are using the above one at all.

Thanks.


Post a Comment for "Adding A Text Input Field Through Js Or JQuery When Clicking On Another Input Field In Rails 3"