views:

17

answers:

1

I am trying to load content from the database using simpletip. My code:

<script type="text/javascript">

    $(document).ready(function(){
        $('a.regularsList').simpletip({
            onBeforeShow: function(){
                this.load('/regulars/tooltip', {id: $(this).attr("id")});
            }
        });
    })

I get that id is undefined. I don't understand what am i doing wrong since i am trying to access the value of the id attribute.

+1  A: 

In your current code this refers to document, instead use .each() loop here so that this refers to the anchor you want, like this:

$(function(){ //short for $(document).ready(function(){
  $('a.regularsList').each(function() {
     var a = this;
     $(this).simpletip({
        onBeforeShow: function(){
          this.load('/regulars/tooltip', { id: a.id });
        }
    });
});

Inside the .each() loop, this refers to the current a.regularsList element you're looking over, so you can just use this.id to get the id property.

Nick Craver
Still it causes the same problem.
no matter if i use this.id or $(this).attr('id') i still get undefined in firebug console.
@user253530 - woops, `this` refers to their object inside that function, need to store a reference, check the updated answer :)
Nick Craver
My code: <a class="regularsList" id="<?php echo $this->id;?>" href="/regulars/regulars/id/<?php echo $this->id;?>"><?php echo $this->name;?></a> I'm using partialLoop in zend_framework to display all the links.
yes, the reference solved it. thank you!
@user253530 - welcome :)
Nick Craver