views:

180

answers:

4

I have a jquery function ('rater') that I call this way:

<div id="someID"></div>

<script type="text/javascript">
    $('#someID').rater({options});
</script>

I want to get the ID ('someID' in this case) so then I can use it inside the function as a variable.

Then I use it in something like this:

if (??? == 'someID') { do something }

How can I do it??

+1  A: 

Are you looking for selector?

After reading your edited question again, it sounds as though you want the string of the id passed through.

<div id="someID"></div>

<script type="text/javascript">
    $("#someID").rater({options},$("#someID").selector);
</script>

For efficiency sake, you can store the $("#someID") off in a variable if you like so you only do the query once.

Nosredna
I think so.. How I use it?
Jonathan
There are examples on the page I linked to.
Nosredna
I mean, I need tho check if the 'selector' is == to something..if (selector == 'some text') { do something }
Jonathan
selector should be able to get what you want. But I'm at a loss of where you want to put your check. Can you write a bit more code in your question to show what you want to do? It doesn't have to work--just give us a hint.
Nosredna
Why the heck is my answer voted down? He's asking for the string of the selector. That's exactly what jQuery's "selector" is for.
Nosredna
Is this some kind of practical joke?
Nosredna
I didn't vote you down, but I suspect he really just wants to find the ID of the element and is going along with the selector stuff because he doesn't know any better. +1 anyways.
Paolo Bergantino
@Paolo. OK. At least that makes sense. I shouldn't have answered what he asked. I should have answered what he should have asked. :-)
Nosredna
+4  A: 

Not sure why you're trying to do this, but...

// create jQuery plugin method: "rater"
jQuery.fn.rater = function() 
{
  // `this` is a jQuery object: process each matched element
  return this.each(function()
  {
    // `this` is a single matched element. Process it, somehow...
    if ( this.id == "someID" )
    {
      // do something special
    }

    // normal rater logic, whatever that might be
  });
};
Shog9
+5  A: 

Retrieving the selector used to call the plugin:

jQuery.fn.myPlugin = function() {
    alert( this.selector );
};

You can access the selector as a property of the jQuery object.

So, if I called the plugin like this:

$('div #something').myPlugin();

Then "div #something" would be alerted.


Retrieving an element's ID from within a plugin:

jQuery.fn.myPlugin = function() {
    alert( this.attr('id') );
};
J-P
+1 This question turned into a circus fast.
Paolo Bergantino
Matt, what on earth are you talking about? The answer you originally posted was 100% incorrect and was simply misleading - the answer I've posted DOES work... If you really think they are "functionally equivalent" then you need to learn jQuery's API properly.
J-P
My apologies, I obviously completely missed the point :)
Matt
A: 
if( $(this).attr('id') == 'someID' ) {
  // do stuff
}
lyrae