2

I have this button, and when clicked, it sends an ajax call which changes the value of score_up.

I dont see what the problem is. I tried firbug, but apparently it's not even detecting the javascript? :)) Thanks.

jquery:

$('.stats').delegate('.support', 'click', function(e) {

      //stop event
      e.preventDefault();
      //get the id
       var the_id = $(this).closest('.score').attr('id').split('_').pop();
        //the main ajax request
       $.ajax({
           context:this,
           type: "POST",
           data: "action=support&id=" + the_id,
           url: "ajax/sa.php",
          success: function (msg) {

               $(this).siblings("h2.score_up").html(msg).fadeIn();
                //remove down button
                // and remove support button


           }
      });
   });

html:

 <ul class="stats">
        <li id="support_23"class="score">
            <h2 class="score_up">12</h2>
            <span style="text-align:center;">Supporters</span>
        </li>
        <li>
    <button type="submit" value="Actions" class="support" title="support">
        <i></i>
        <span>Support</span>
    </button>
</li>


//vote down button

 <li id="down_23"class="score">
            <h2 class="score_down">12</h2>
            <span style="text-align:center;">down</span>
        </li>
        <li>
    <button type="submit" value="Actions" class="down" title="down">
        <i></i>
        <span>down</span>
    </button>
</li>

    </ul>
2
  • Please be more specific about the problem itself if not it might be very difficult to help you, help us help you! Commented Oct 17, 2010 at 21:45
  • the problem is when i try to click the support button, on the html, its meant to send an ajax call then update the h2.score_up on the html,in this case to 13, but its doing nothing, its not even posting! :)) Commented Oct 17, 2010 at 21:48

2 Answers 2

2

It is not valid HTML for a <button> to be a direct child of a <ul>.

Children of <ul> should be <li>. I wouldn't expect things to work properly with invalid HTML.

HTML with <button> inside a <li>:

<ul class="stats">
    <li id="topic_23" class="score">
        <h2 class="score_up">12</h2>
        <span style="text-align:center;">Supporters</span>
    </li>
    <li>
        <button type="submit" value="Actions" class="support" title="support">
            <i></i>
            <span>Support</span>
        </button>
    </li>
</ul>

jQuery, fixing some of the traversal methods:

$('.stats').delegate('.support', 'click', function(e) {
      //stop event
      e.preventDefault();
        // cache a reference to the previous <li> element
        //   since it is used more than once
       var $prevLi = $(this).closest('li').prev('li');
      //get the id
       var the_id = $prevLi.attr('id').split('_').pop();
        //the main ajax request
       $.ajax({
           context:this,
           type: "POST",
           data: "action=support&id=" + the_id,
           url: "ajax/sa.php",
          success: function (msg) {
               $prevLi.find("h2.score_up").html(msg).fadeIn();
           }
      });
 });
Sign up to request clarification or add additional context in comments.

22 Comments

hey patrick, what do you think i should do then, this is a bit different from the other one we did? when we were working with links, im trying to do this with buttons for my blog!!!
@getaway - Did this code work? I placed the <button> in its own <li>. Not sure if that's how you want it. We could place it in the first <li>, but will need to change a little then.
parick it works fine , but instead of chnaging the score up, it removes it
@Peter - You're welcome. I personally think live() is overused/over-recommended. Typically dynamic events are added to some specific container that can have a delegate. I personally wouldn't use .live() unless there's some sort of element that could be placed at any location on the page. I don't think that happens too often.
@getaway - From the button, traverse up to its <li> then over to the proper <li> that has the other button, then .find() the button. Then remove the original button. Something like this: $(this).closest('li').next().next().find('button').remove(); $(this).remove();
|
2

Your HTML is invalid, so I would do:

<form action="javascript:alert('form')";>
<ul class="stats">
    <li id="topic_23"class="score">
        <h2 class="score_up">12</h2>
        <span style="text-align:center;">Supporters</span>
    </li>
    <li>
      <button type="submit" value="Actions" class="support" title="support">
      <i></i><span>Support</span></button>
    </li>
</ul>
</form>

And then the jQuery (which in your original would not work, since you wanted .siblings() and not .closest() would now be:

var the_id = $(this).closest("li").siblings('.score').attr('id')
                    .split('_').pop();

and success:

$(this).closest("li").siblings("li.score").find("h2.score_up")
       .html(msg).fadeIn();

I think you also run into troubles with prevent default, since you want to prevent default on the form, and in that case you might run into problems with delegate.

Here is what I would do with .live():

// Prevent form from submitting
$("form").live("submit", function(e) {
    e.preventDefault(); });

// Run Ajax
$('button.support').live('click', function(e) {
      //get the id
       var $score = $(this).closest("li").siblings('.score'),
           the_id = $score.attr('id').split('_').pop();
        //the main ajax request
      $.ajax({
           context:this,
           type: "POST",
           data: "action=support&id=" + the_id,
           url: "ajax/sa.php",
           success: function (msg) {
               $score.find("h2.score_up").html(msg).fadeIn(); 
           }
      });
});

Try it out on this jsFiddle

5 Comments

@getaway, your success isn't quite right either. Was still in the process of editing. Ok, added in the corrected succes callback too.
thank you very much, it solved the ajax bit, pheeeeeew, thanks again +uovote from me, but its not updating the score, i get the right reponse from firebug, but it deosnt not update the score on the webpage (H2.support bit) :))
@getaway - I think there's something odd going on with your .preventDefault(), since you want to prevent the default on submit not click. The way I know how to do that is with live(). I put together a working example on jsFiddle. You might be able to use delegate on the click sitll, but I was having problems with it.
it works on js fiddle but not on my page, i tried your alternative but its still the same thing
@getaway - I also added a little cleanup to your HTML. Since you had invalid HTML that may have also been causing problems. Try it now. Also, if you can get it to work on jsFiddle but not your page, that means your page is somehow different.

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.