0

I'm quite new to jQuery, so still trying to wrap my head around how it works.

I'm creating a admin-panel where the admin can insert some questions. As its not a fixed amount, the admin should be able to add or remove extra input-fields, so the admin can choose the desired amount questions.

This is how it looks graphically: Add-remove

And this is the code:

            <div class="adinputfield90">
            <input type="text" placeholder="Insert question 1"> <span class="font-entypo icon-circled-cross adaddnextremove" aria-hidden="true"></span></input>
            </div>

The span class holds the image, so the add-new input is just another webfont.

How can I get this to work, so I can add an infinite amounts of inputfields? I should of cause make sure that if theres only one input I can only add, and not remove that one.

All help is appreciated!

1

3 Answers 3

0

Include this in head section of the page

<script>
$(function(){

  $('.adinputfield90 span').click(addTextBox);

});

function addTextBox(){
  $('.adinputfield90').append('<input type="text" placeholder="Insert question 1"> <span class="font-entypo icon-circled-cross adaddnextremove" aria-hidden="true"></span></input>');
  $('.adinputfield90 span').unbind('click');
  $('.adinputfield90 span').click(addTextBox);
}
</script>
Sign up to request clarification or add additional context in comments.

Comments

0
 var template='<div class="adinputfield90">';
 template=template+'<input type="text" placeholder="Insert question 1"> 
 <span class="font-entypo icon-circled-cross adaddnextremove" aria-hidden="true"></span>
  </input>';
  template=template+'</div>'


  $("#htmlContainerId").append(template);

htmlContainerId - The container into which we are inserting the content.That will be the element just outside of 'adinputfield90' ie, the container of 'adinputfield90'

Comments

0

Try this.

$('.adaddnextremove').on('click', function(){
    $(this).parent().clone(true).appendTo('body');
    $('.remove').show();
    $('.adaddnextremove').hide();
    $('.adinputfield90').last().find('.remove').hide();
    $('.adinputfield90').last().find('.adaddnextremove').show();
});
$('.remove').on('click', function(){
    $(this).parent().remove();
});

Fiddle Demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.