tags:

views:

31

answers:

3
    $('.more').click(function test(id) {
          $('#MoreFriendInfo'+id).toggle();
   });

I made this, function test(id), now how should i make the link right in html/php in order to grab the id? I tried this, but it doesnt work:

<a class='more' href="javascript:void(0);" onclick='test(<?php echo $showInfo["bID"]; ?>)'>mer</a>

In firebug i get "test isnt defined"

+1  A: 

In all of these, we're removing the onclick from the markup:

You can use a data attribute, like this:

<a class='more' href='#' data-id='<?php echo $showInfo["bID"]; ?>'>mer</a>

Then grab it in your .click() handler, like this:

$('.more').click(function () {
  $('#MoreFriendInfo'+$(this).attr('data-id')).toggle();
});

Alternatively if it's in another container forget the IDs and find it relatively, for example if the markup was like this:

<div class="container">
   <a class='more' href="#">mer</a>
   <div class="moreFriendInfo">Content</div>
</div>

You could do it like this:

$('.more').click(function () {
  $(this).closest('.container').find('.moreFriendInfo').toggle();
});

(In this case you can actually use .siblings(), but it's meant to be a more general approach)

Nick Craver
Thanks while waiting on accepting your question i got an question. Can you specify any attr name you want? (to your first solution)
Karem
@Karem - They should start with the `data-` prefix, other than that yes, whatever you want, they don't do any harm in HTML4, and they're actually part of the spec in HTML5.
Nick Craver
Thank you!.....
Karem
+1  A: 

The way you are defining it, test(id) is an inner function that is only scoped within .click().

Note that when you use .click(), you are effectively defining onclick. You don't need to add it to the markup also.

Rather than passing the id, I would design the markup in such a way that the elements were placed in such a way that I could use selector context to pass a starting point for the selector.

$('.more').click(function () { $('.moreFriendInfo',this).toggle(); });

rchern
This approach would find a `class="moreFriendInfo"` element *inside* the anchor, which doesn't appear to be the case :)
Nick Craver
@Nick, please see what I said above the code "rather than passing the id". The code could also use a selector of $(this).parent(). **There is no reason to do a full dom search in a case like this.**
rchern
@rchern - I never said search the entire DOM :) See my answer, something like `$(this).siblings()` or `$(this).closest('selector').find('selector')` is what I was talking about.
Nick Craver
@Nick, I understand, but my answer already mentioned looking at the design of the markup prior to your comment. Using $(this).closest('selector').find('selector') seems like it isn't the most optimized way.
rchern
@rchern - It goes up to the container and finds the "more" to show within the container you're in...if you have say 200 of these on a page....you have a more optimal way? :) The point was to find it relatively, in as few steps as possible, of course I agree to not do any extra work. I was simply commenting that a `this` context selector is hardly ever applicable in a "view more" context....I've never seen the "more" also contained *inside* the link :) (though it's surely possible...)
Nick Craver
@Nick, I've seen code (and not mine :p) that puts the click event on a container div if that div is only going to contain the single link with its 'more' container. The event bubbles up from the a to the container div and `this` works as a selector.
rchern
A: 

Nick's answer is almost perfect, but I'd use "rel" instead of "data-id" unless data-id is a valid HTML attribute.

Moe Sweet
They are valid attributes, `data-anything` is reserved for those attributes you wish were there but never had :)
Nick Craver