views:

67

answers:

3
+2  Q: 

jquery problem?

i have this jquery problems i have had for days, im so new to this, so excuse my dumbness in this subject. thanks :))

html code:

<ul class="statuses">
<li id="set_23" class="message">
  // this is meant to increase everytime you click the vote_up just like stackoverflow 
  <span class="vote_count">27</span>
  <a href="#" class="vote_up"><img src="img/uparrow.png" /></a>
  <a href="#" class="vote_down"><img src="img/downarrow.png" /></a>

 </li>

</ul>

this is my jquery code:

$(document).ready(function() {
    $('#statuses').delegate('.vote_up', 'click', function() {
       //get the id
        var the_id = $(this).closest('.message').attr('id').split('_').pop();
        //the main ajax request
        $.ajax({
            type: "POST",
            data: "action=vote_up&id=" + the_id,
            url: "ajax/votes.php",
            success: function (msg) {
                  // fade in the new vote_count
                $(this).siblings("span.vote_count").html(msg).fadeIn();
                  // get the child <img> and set its src
                $(this).children("img").attr("src", "img/uparrowActive.png");
            }
        });
    });

});

EDIT: also the jquery fade.in and change of image src is not working, am i referencing the right span or class(the ajax request is working now)

A: 

Change this

var the_id = $(this).closest('.message').attr('id').split('_').pop();

to this

var the_id = $(this).parents('.message').attr('id').replace('set_', '');

or alternatively

var the_id = $(this).parent().attr('id').replace('set_', '');

if you know that your link's parent element will always be ul.message.

mway
cheers for that!! :))
getaway
Why the change? `.parents()` could return more than one result if there are nested `.message` elements, while `.closest()` will guarantee only the first ancestor with `.message`. And there's nothing wrong with `.split('_').pop()`.
patrick dw
Personal preference to be as explicit as possible. `.closest()` introduces ambiguity and could potentially result in bad results if markup is changed (whereas being explicit requires JS to be conscious of the element hierarchy).
mway
Also: keeping function calls to a minimum, especially when chaining, is never a bad idea.
mway
Ok, personal preferences. I just couldn't figure out how your answer related to an issue in the question's code.
patrick dw
+5  A: 

First thing to jump-out:

<ul class="statuses"> 

and

$('#statuses')

... the # symbol in jQuery is the id-selector (same as CSS). Since your <ul> is set to class="statuses" then you'll need to use the class-selector:

$('.statuses')

That alone will break all your jQuery

STW
d'oh, can't believe I didn't see that.
mway
lol thanks, the vote_count is working, which is brillant, :)), but the success function is not performing the fade.in and the change of image!! +upvote from me thhanks STW
getaway
np; usually it's the simple things like that
STW
Or if there's only one `statuses` on the page (I'm guessing that's the case) it would be better to keep the `#statuses`, and change the element to `<ul id="statuses"> `. This will be a much faster lookup. Again, this is only if there's only meant to be one on the page.
patrick dw
everything works now thanks guys, but howcome when i click on vote button on the buttom of the page it takes me to the top of the page?
getaway
@getaway - Add `return false;` to the click handler, or add the `e` parameter to the handler: `.vote_up', 'click', function( e ) {...` and call [`e.preventDefault()`](http://api.jquery.com/event.preventdefault/) inside the handler in order to prevent the default behavior of the `<a>` element from triggering.
patrick dw
cheers, you guys are like geniuses or what, how long have you been doing this ? :)) thank you very much @patrick uve been the heart of my problems!!!
getaway
@getaway - Keep at it. It'll start to feel natural before you know it. :o)
patrick dw
+1  A: 

From the jQuery docs:

The beforeSend, error, dataFilter, success and complete options all take callback functions that are invoked at the appropriate times. The this object for all of them will be the object in the context property passed to $.ajax in the settings; if that was not specified it will be a reference to the Ajax settings themselves.

So, try adding context: this to your ajax request options.

FRotthowe