1
    $('.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"

3 Answers 3

1

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)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks while waiting on accepting your question i got an question. Can you specify any attr name you want? (to your first solution)
@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.
1

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(); });

6 Comments

This approach would find a class="moreFriendInfo" element inside the anchor, which doesn't appear to be the case :)
@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 - 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, 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 - 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...)
|
0

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

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.