On Wed, Jul 2, 2008 at 11:53 AM, lwoods <[EMAIL PROTECTED]> wrote:

>
> Why can't I reference the DIV using the jQuery format?  Here is the
> example (minus opening HTML):
>
> <script language="javascript" src="js/jquery-1.2.3.pack.js"></script>
> <script language="javascript">
> <!--
> $(function(){
>        $("#button").bind('click',filldiv);
> });
> function filldiv(){
>        // $('#info').innerHTML='Test';  <--- This doesn't work!


$("#info") returns a jQuery object, an array-like collection of DOMElements
with all the jQuery methods. You're trying to access a DOMElement property
(innerHTML), but you're doing it on the jQuery object. If you want to get at
the DOMElement, you can use array index notation, like so:

$("#info")[0].innerHTML = 'Test'; // [0] since you want the first/only
element returned

But where's the fun in that? You're only using jQuery to select the element.
You might as well use document.getElementById ;). Plus, the query might not
return any elements (if no element has that id) and then you'll get an
exception like this:

'$("#info")[0] is undefined'

which means to be safe you have to check whether the element exists, etc.
Yuck. jQuery's .html() method takes care of all of this for you:

$("#info").html('Test');

It will set the html on 1 or more elements (all that match your query), and
it runs quite nicely on 0, with no exception.

- Richard

Reply via email to