views:

96

answers:

2

Hi, I am looking for a way to debug what a jquery selector returns/selected. I tried to use toString() but that only returns [object Object].

What I am actually trying to do is to attach a callback to radio buttons. And on click on one of the buttons I want to submit the enclosing form.

Therefore I try to do something like this:

        $(".rating").stars({
            cancelShow: false,

        callback: function(ui, type, value, event)

        {
                $(this).closest('form').ajaxSubmit();

        }

        });

But unfortunately just nothing happens.

Thanks a lot, Fabian

Here is a complete example of what I try to do (sorry the code seems ti get messed up):

Scripts used:

    <script type="text/javascript" src="http://localhost:8000/media/js/jquery.form.js?v=2.4.3"&gt;&lt;/script&gt;
    <script type="text/javascript" src="http://localhost:8000/media/js/jquery-ui.custom.min.js?v=1.8"&gt;&lt;/script&gt;
    <script type="text/javascript" language="javascript" src="http://localhost:8000/media/js/jquery.ui.stars.js?v=3.0.0b38"&gt;&lt;/script&gt;
    <link rel="stylesheet" type="text/css" media="all" href="http://localhost:8000/media/css/jquery.ui.stars.css?v=3.0.0b38" />        


<body>

<script type="text/javascript">
    $(function(){
        $(".rating").children().not(":radio").hide();
        $(".rating").stars({
            cancelShow: false,
            callback: function(ui, type, value, event)
            {
                alert('NodeName: ' + this.nodeName);
                $(this).each(function() {
                    alert($(this).html());
                });

                alert($(this).length);
            }
        });
    });
 </script>

    <ul class="list">
    <li>
        <form class="rating" id="rating-1" action="/sniphunter/rate/1/" method="post">
            <input type="radio" name="score" value="1" id="rating-1-1" />
            <input type="radio" name="score" value="2" id="rating-1-2" />
            <input type="radio" name="score" value="3" id="rating-1-3" />
            <input type="radio" name="score" value="4" id="rating-1-4" />
            <input type="radio" name="score" value="5" id="rating-1-5" />
            <input type="submit" value="Rate"/>
    </form>
    </li>
    </ul>

A: 

You can use .length to see if they found anything (the most common case), like this:

$(".rating").stars({
  cancelShow: false,
  callback: function(ui, type, value, event) {
    alert($(this).closest('form').length);
    $(this).closest('form').ajaxSubmit();
  }
});

There are other options, for example if you wanted to iterate each one and spit out it's html so you know what it found you can use .each() and .html():

$(".rating").stars({
  cancelShow: false,
  callback: function(ui, type, value, event) {
    $(this).closest('form').each(function() {
      alert($(this).html());
    });
    $(this).closest('form').ajaxSubmit();
  }
});

These are two quick examples out of dozens, but usually .length shows the issue, e.g. this not being what you want it to be in that callback.

Nick Craver
Thanks this already help that $(this) has length 1 and all my tries of different slectors has length 0.
Fabian
@Fabian - Try `alert(this.nodeName);` see if it's even the element type you're after?
Nick Craver
ohh well nodename is undefinded, so I really need a way to debug this properly :-(
Fabian
@Fabian: Share some HTML and it might help us troubleshoot your problem better
fudgey
@Fabian - `$(ui).closest('form').ajaxSubmit();` may be what you're after.
Nick Craver
Just to let you know, I edited my original question with a complete html example.
Fabian
A: 

I downloaded the Star Rating Widget (it looks like the same one you are using) and did some digging... the stars plugin completely removes the radio buttons and replaces them with div's and links. When you select a star, it saves the value in a hidden input inside the form.

So, after messing around with it, I found that you can get to the form using ui.$form, so try this:

$(function(){
    $(".rating").children().not(":radio").hide();
    $(".rating").stars({
        cancelShow: false,
        callback: function(ui, type, value, event)
        {
            alert( ui.$form.attr('id') );  // alerts the form ID
            ui.$form.ajaxSubmit();
        }
    });
});

Edit: Oh and if you want to access your original radio buttons, they are saved into the ui object as well inside of ui.$rboxs. The following snippet will alert the ID of the original radio button:

$(function(){
    $(".rating").children().not(":radio").hide();
    $(".rating").stars({
        cancelShow: false,
        callback: function(ui, type, value, event)
        {
            alert( ui.$rboxs[(value-1)].id );
        }
    });
});
fudgey
Thanks a million, that worked like a charm. Any hints how I could debug this myself the next time? I tried to use firebug, but still had no clue where I was in the DOM and what my selector was selecting etc...Anyways many thanks for the help!
Fabian
Actually I used firebug to figure it out. Instead of `alert( ui )` use `console.debug( ui )`. It will display the object (clickable and opens into a tree view) in the console that you can navigate through.
fudgey
Thanks for the tip with the console, I always got "not defined" and didn't try further. The solution was to open the console Tab and now it works and I see the tree view and the form element :-)
Fabian