1

i am writing following javascript application to get the data from user. When user select the values from select menu, it show different form elements, based on the option value. I have following code

HTML

<select>
    <option value="">hi</option>
    <option value="1">1</option>
</select>
<div></div>

Javascript

var myFunc = function () {
    var string = '<input><span>+</span>';
    $(string).appendTo('div');
    $('span').on('click', function () {
        myFunc();
    })
};


$('select').on('change', function () {
    var value = $(this).val();
    if (value == 1) {
        myFunc();
    }
})

My question is when the user click on the plus icon it should show another input element with the plus mark. when user select the last plus mark same thing should happen.
but the previous plus marks should become a minus. and when the user click on minus all the element below to the that minus mark should be removed.

currently my code generates the input elements. but i dont understand the way of adding a minus marks and removing other elements. and also i need to limit the number of input elemnts to 5. Please help me. :)

DEMO

1

1 Answer 1

1

You can try this:

var myFunc = function () {
    var string = '<input /><span>+</span>';
    $('div span').html('-');
    $(string).appendTo('div');
    $('span').click(function () {
        if ($(this).text() == '-')
        {
            $(this).nextAll().remove();
            $(this).html('+');
        }
        else
            myFunc(); 
    });
};


$('select').on('change', function () {
    var value = $(this).val();
    if (value == 1) {
        myFunc();
    }
})

Fiddle

Sign up to request clarification or add additional context in comments.

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.