2

I want to execute some functions on a selected element $(selected), using this as the selector.

$(external).click(function(){
  //relevant code below
  $(selected).????(){
    console.log($(this).html()); //i.e.
    console.log($(this).height()); //i.e.
  }
});

Basically, I don't know what to put for in place of ???? above. Any ideas?

Edits * reworded question, and added sample code for clarity.

3
  • $(document).ready(function(){ //code to be fired on page load }); Commented Jun 8, 2011 at 5:01
  • Not sure I understand. Do you mean just executing a JavaScript function or do you mean execute a function on a set of elements returned from a jquery selector? Commented Jun 8, 2011 at 5:03
  • Can you explain in more detail? When do you want your function to be executed? Commented Jun 8, 2011 at 5:04

3 Answers 3

9

If you want to execute a function after the page is loaded with jquery you can do it like

$(function(){
    //code
});

Or if you want it executed right away:

(function(){
    //code
}());
Sign up to request clarification or add additional context in comments.

Comments

5

I think you're after .each()

$(external).click(function(){
  //relevant code below
  $(selected).each(function(){
    console.log($(this).html()); //i.e.
    console.log($(this).height()); //i.e.
  });
});

Doc: http://api.jquery.com/each/

Comments

1

Using external as the element:

<div id="external">
    <div id="internal">
        inner
    </div>
</div>

Then for original element:

$(document).ready(function(){
var external = $("#external");
    external.click(function(){
      var selected = $(this);
        console.log(selected.html()); //i.e.
        console.log(selected.height()); //i.e.
      });
});

Or element contained within:

$(document).ready(function(){
var external = $("#external");
    external.click(function(){
      var selected = $(this).find("#internal");
        console.log(selected.html()); //i.e.
        console.log(selected.height()); //i.e.
      });
});

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.