2

I'm trying to pass a jQuery selected object into a function, and the function will do a .html() on it.

Html:

<div id="box">this is a box</div>

Js:

var fillBox = function(box, text) {
  box.html(text);
}

(function() {
  var box = $("#box");
  fillBox(box, "new text");
});

The box stays as "this is a box" and never gets updated, even tho the function is called. I even tried $(box).html(text) inside the function, but that doesn't fix it. Is this do-able?

http://jsbin.com/iqixoj/5/edit

1
  • 1
    Because the code runs before the element is rendered on the page! Commented Jan 31, 2013 at 20:06

1 Answer 1

8

You forgot a $.

$(function() {
  var box = $("#box");
  fillBox(box, "new text");
});
Sign up to request clarification or add additional context in comments.

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.