1

I have the following function which works very well within a $(document).ready(function(){

$('.threadWrapper > .littleme').click(function() {
        $(this).next().toggle();
        $(this).toggle();
        $('.littleme').not(this).next().hide();
        $('.littleme').not(this).show();

        // re-run masonry
        $('#mainContent').masonry();

        return false;
    }).next().hide();

What I want to be able to do is call this from inline javascript Each div element that contains the threadWrapper class also has its own id. What I want to do is to be able to trigger this function using inline javascript calls and sending an id as a parameter. For example:

$(function(id){
$('#id > .littleme').next().toggle();
$('#id > .littleme').toggle();
etc. etc.
});

3 Answers 3

2

Just concatenate the strings for the selector like this:

function toggleStuff(id) {
  $('#' + id + ' > .littleme').next().toggle();
  $('#' + id + ' > .littleme').toggle();
  //etc. etc.
}

Define this outside your document.ready function so it's available. Also, you can shorten this if you want as well down to:

  $('#' + id + ' > .littleme').toggle().next().toggle();
Sign up to request clarification or add additional context in comments.

Comments

1

You can just use a regular javascript function:

function Something(id) // id is a jquery object
{
  id.find('.littleme').next().toggle();
  id.find('.littleme').toggle();
  etc. etc.
}

Comments

0

There is no need to call any inline javascript from your elements. What I would recommend is the following:

$('.threadWrapper').click(function() {
    $(this).find(".littleme").next().toggle();
    $(this).find(".littleme").toggle();
    etc. etc.
});

Looking at your selectors, .littleme is a direct descendant of .threadWrapper. So if you bind a click event to all .threadWrappers you can select the .littlemes by searching within the current context.

Also, if you're interested, those two statements can be consolidated using .end:

$(this).find(".littleme").next().toggle().end().toggle();

Comments

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.