views:

3999

answers:

1

I have a PHP file which will return something like

<div id="title">Add Us On Myspace!</div><img id="image" src="http://i39.tinypic.com/67jybs.png" alt="Add Us On Myspace!" />

This is called using this...

$.ajax({
    url: "Tabnews.php",
    success: function(tab1html){
            $("#tabs-1").html(tab1html);
    }
});

I need to define the contents of #title and #image in a variable in jQuery so I can write them else where.

How can I do this?

+4  A: 

This should do it:

var $html = $(tab1html); // parse string into DOM structure
var $title = $html.filter('#title'); // keep the title
var $image = $html.filter('#image'); // keep the image

// add to whatever elements we want...
$('#my_element').append($title); 
$('#my_other_element').append($image);
Paolo Bergantino
why do your javascript variables have a $ prefixing them? I wouldn't recommend that unless you're advocating some hungarian style notation.
Paul Tarjan
@Tarjan: It is common practice to give jQuery objects a $ prefix to denote that they are a wrapped set you can do jQuery actions on. This is considered a good thing and is far from bad practice.
Paolo Bergantino
agreed, it allows you to easily understand what is a jq obj as opposed to a dom obj
redsquare
I love the $ prefix, you sir have just changed the way I name my jQuery objects :)
rball