0

I am connecting this javascript to server code, but I am having trouble selecting the right element. Regardless of what button I select, the video_id always ends up being the first one (in this case "bbc"). How do I change the javascript/jquery to select the right value for video_id depending on the button selected?

    <script type="text/javascript">
   $(document).ready(function() {
       $(".removebutton").submit(function(event){
       event.preventDefault();
            $.ajax({
                 type:"POST",
                 url:"/munch_video/",
                 data: {
                        'video_id': $('#video_id').val(), // from form
                        'playlist': $('.playlist').val(), // from form
                        'add_remove': $('.add_remove').val(), // from form 
                        },
                 success: function(message){                         
                        alert(message);
                        $('.span8').html(message);
                    }
            });
            return false;
       });

    });
</script>


        <form method='post' action = '/munch_video/ ' class = 'removebutton'>{% csrf_token %}
            <input type="hidden" value="Channel" class = "playlist"/>
            <input type="hidden" value="bbc" id = "video_id"/>
            <input type="hidden" value="remove_video" class = "add_remove"/>

            <input type='submit' class="btn btn-danger" value='Remove from plate'/>
        </form>

        <form method='post' action = '/munch_video/ ' class = 'removebutton'>{% csrf_token %}
            <input type="hidden" value="Channel" class = "playlist"/>
            <input type="hidden" value="toyota" id = "video_id"/>
            <input type="hidden" value="remove_video" class = "add_remove"/>

            <input type='submit' class="btn btn-danger" value='Remove from plate'/>
        </form>

        <form method='post' action = '/munch_video/ ' class = 'removebutton'>{% csrf_token %}
            <input type="hidden" value="Channel" class = "playlist"/>
            <input type="hidden" value="gm" id = "video_id"/>
            <input type="hidden" value="remove_video" class = "add_remove"/>

            <input type='submit' class="btn btn-danger" value='Remove from plate'/>
        </form>

        can be multiple buttons, each with a different video_id value
2
  • 1
    ID's must be unique. This will not work. Commented Dec 5, 2012 at 4:22
  • I changed the video_id to class instead of id in my code now. Commented Dec 5, 2012 at 4:34

2 Answers 2

2

The problem is because you have multiple elements with the same id. Hence the same element being picked.

Since you are working on the remove button clicks, you can do a find within like

'video_id': $(this).find('#video_id').val()
Sign up to request clarification or add additional context in comments.

Comments

1

This selector $('#video_id') gives you collection matching the selector but using val() on it always applied to first element. You can pass this as context with selector to get video_id with in descendants of the current form.

Change

$('#video_id').val()

To

$('#video_id', this).val()

or use find() method to search the id in descendants

$(this).find('#video_id').val();

Edit to remove the clicked form in succcess

$(document).ready(function() {
   $(".removebutton").submit(function(event){
   currentForm = $(this);
   event.preventDefault();
        $.ajax({
             type:"POST",
             url:"/munch_video/",
             data: {
                    'video_id': $('#video_id').val(), // from form
                    'playlist': $('.playlist').val(), // from form
                    'add_remove': $('.add_remove').val(), // from form 
                    },
             success: function(message){                         
                    alert(message);
                    $('.span8').html(message);
                    currentForm.remove();
                }
        });
        return false;
   });

});

4 Comments

Great, this worked...thanks! Another question: do you know how I would then remove the element that was selected on success. Since the form is wrapped in div class = "span8" and because message returns the value of the video_id back again, I tried $('.span8').removeClass(message)...but it doesn't seem to work.
You assigned the received response to .span8 now what element you want to remove from it?
The selected button. If I selected the "gm" button for example, I would like to remove that form/button.
I don't think I did a good job of explaining my question. stackoverflow.com/questions/13716545/…

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.