0

I'm creating a sitemap function for my website what I have a function which appends a new select box which multiple options and I have a remove function which will remove the select box.

Well I say remove the select box but what it actually does is remove all the select boxes that were created, however I wanted it to target the select box that it is related to.

I believe one way to implement this is to assign a different class to the select box element, does anyone know how I can do this?

Or can anyone recommend a better way to handle this?

The code I have so far is below, or view my jsFiddle

$("#newsublevel").click(function() {
        $(".navoptions").append('<br/><select class="newoption"><option value="Home">Home</option><option value="Home">Home</option><option value="Home">Home</option><option value="Home">Home</option><option value="Home">Home</option></select><a href="#" class="remove">Remove</a>');
    });

$(".navoptions").on('click','.remove',function() {
    $(".newoption, .remove").remove();
});

add.html

<div class="maincontent">
<h2 class="sitemaphead">Sitemap</h2>
<p>Add a sub level to About.</p>
<div class="navoptions">
<select>
<option value-"Home">Home</option>
<option value-"Home">Home</option>
<option value-"Home">Home</option>
<option value-"Home">Home</option>
<option value-"Home">Home</option>
<option value-"Home">Home</option>
<option value-"Home">Home</option>
</select>
</div>
<p id=""><a href="#" id="newsublevel">Click here</a> to add another sub level</p>

</div>

4 Answers 4

3

this should work

$("#newsublevel").click(function() {
    $(".navoptions").append('<div><select class="newoption"><option value="Home">Home</option><option value="Home">Home</option><option value="Home">Home</option><option value="Home">Home</option><option value="Home">Home</option></select><a href="#" class="remove">Remove</a></div>');
});

$(".navoptions").on('click','.remove',function() {
    $(this).closest('div').remove()
});

i have added a container div element. on clicking the remove the code will find the container element and will remove only that

here is the updated jsfiddle http://jsfiddle.net/8ddAW/6/

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

2 Comments

thanks for your input @Parv however this still removes each select box, I want it to remove the select box which I have clicked remove on, therefore I believe I need a unique identifier
Looks like this would work to me because the added select gets wrapped in a div, could even add a class to that div if needed for clarity.
2

Try this:

$(".navoptions").on('click','.remove',function() {
    $(this).prev().andSelf().remove();       
});

Fiddle Here

1 Comment

This leaves the extra <br /> in that got added
1

Try this out:- http://jsfiddle.net/adiioo7/8ddAW/4/

JS:-

$(function() {

        $("#newsublevel").click(function() {
            $(".navoptions").append('<div><br/><select class="newoption"><option value="Home">Home</option><option value="Home">Home</option><option value="Home">Home</option><option value="Home">Home</option><option value="Home">Home</option></select><a href="#" class="remove">Remove</a></div>');
        });

    $(".navoptions").on('click','.remove',function() {
        $(this).parent().remove();
    });

});

2 Comments

that worked thank you however I've accepted an answer below but will still +1 you as it is an equally good answer!
No need for the <br /> if it gets wrapped in a block element like a div it would seem
0

Actually, by using the fieldset element inside your form, you can perform grouping of all the <select> statements that you would like to be "related" and control them all at once. Even deeper, if you can keep track of the indexes of the groups that you're dynamically adding to, you could use those same indexes to clear them out without having to differentiate between them using unique classes.

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.