Understanding jquery Chaining
My question concerns the chaining of commands / events in jQuery.
In short, I'm trying to grab the 'title' attribute from an image, and display that as the caption for the image. I'm doing this by using jquery's .attr('title') and .appendTo() events. The following code is what is accomplishing this functionality:
- $(document).ready(function(){
- var title
- title = $('img.myClass').attr('title');
- $('div.append-here').append(title);
- });
BUT ... for the sake of learning and "Write Less ... Do More" - could someone tell me why the following doesn't work:
- $(document).ready(function(){
- $('img.myClass').attr('title').appendTo('div.append-here');
- });
I'm not sure why that won't work. In the same way - this doesn't work:
- $(document).ready(function(){
- $('div.append-here').append($('img.myClass).attr('title'));
- });