Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Exploding each tag from Ajax Response in jQuery.
Old 06-23-2011, 04:27 AM Exploding each tag from Ajax Response in jQuery.
phazorRise's Avatar
Skilled Talker

Posts: 57
Name: Sachin Gutte
Trades: 0
hi there,
I've collected an ajax response in msg variable.
The type of data received from ajax is of type HTML.

see below:
Code:
$(".test").click(function(event){
       $.get('temp.htm',success,typeofdata);
       var success=function(msg){

            //here want to extract the contents of msg.
            //eg. if msg='<div id="image"><img src="example.gif"></img></div>';
           //then i want to operate on $('#image') from above before 
           //putting it into another element

            $('#dest').html(msg);			
       }
       event.preventDefault();
});
So how can i select perticular DOM element from the response received.
Any guess?
phazorRise is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-23-2011, 08:25 AM Re: Exploding each tag from Ajax Response in jQuery.
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Hmm. Well, when adding new elements to the DOM you can do like this:

Code:
var image = $('<div id="image"><img src="example.gif"></div>');
image.some_jquery_function();
$('#dest').append(image);
So possibly, you could just replace the first line with

Code:
var image = $(msg);
And when inserting the element you can choose what ever function you want of course, like append(), prepend(), insert(), insertBefore() etc. depending on where you want it. See the jQuery documentation.
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is online now
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 06-23-2011, 01:19 PM Re: Exploding each tag from Ajax Response in jQuery.
phazorRise's Avatar
Skilled Talker

Posts: 57
Name: Sachin Gutte
Trades: 0
that's right that i'm adding new DOM elements into document.
But, only those elements are added which are retrieved from ajax response.

In addition, the main motive is to operate on those elements before adding them into a page. Operating means like finding dimensions,inner html and other stuff like using those elements as they are like present in same document.

And all this before adding them into DOM.

Ajax Response may vary from images,text and other media.

Eg.
Code:
 
//if ajax response is like-
 msg='<div id="image"><img src="example.gif"></img></div>';
then i would like to change dimensions,inner html/text of suppose $('#dest')
Thank you in advance.

Last edited by phazorRise; 06-23-2011 at 01:20 PM..
phazorRise is offline
Reply With Quote
View Public Profile
 
Old 06-23-2011, 01:39 PM Re: Exploding each tag from Ajax Response in jQuery.
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
I stand by my previous response. Create a jQuery object off it and store it in a variable
Code:
var elem = $(msg);
Then do all the operations you want on it.
Code:
// change src of an image in the element
elem.first().attr('src', 'example2.gif');

// set a class to all image child nodes
elem.children('img').attr('class', 'my_image');

// add some text to the element
elem.append('<p>Some text</p>');

// add some content to a div inside the element and hide it
elem.find('#special_div').html('some content that will be hidden').hide();

// etc.
Although I doubt that you can find dimensions of anything before it is added to the dom.

Then add it to the dom

Code:
$('#dest').append(elem);
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is online now
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 06-23-2011, 01:42 PM Re: Exploding each tag from Ajax Response in jQuery.
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Oh, and by the way. <img> tags does not have an end tag (</img>).
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is online now
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 06-24-2011, 07:18 AM Re: Exploding each tag from Ajax Response in jQuery.
phazorRise's Avatar
Skilled Talker

Posts: 57
Name: Sachin Gutte
Trades: 0
Quote:
Although I doubt that you can find dimensions of anything before it is added to the dom.
hmm, you're right.
And one more question.

I'm getting the cached ajax response.

I tried putting something like this while building querystring to avoid getting cached response-

Code:
var s=Math.random();
var url='some_file.html?id='+s;
$.get(url,success,datatype);
but it's not working.
phazorRise is offline
Reply With Quote
View Public Profile
 
Old 06-24-2011, 09:18 AM Re: Exploding each tag from Ajax Response in jQuery.
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
I've never had that problem, but adding a random id should work. You do generate a new random id for every call, right?
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is online now
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 06-24-2011, 10:57 AM Re: Exploding each tag from Ajax Response in jQuery.
phazorRise's Avatar
Skilled Talker

Posts: 57
Name: Sachin Gutte
Trades: 0
ohh it works. i was'nt passing the url stuffed with random id. to ajax get method. i was using old url variable.
i was kinda fool. thanx for concern.
phazorRise is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Exploding each tag from Ajax Response in jQuery.
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.26886 seconds with 12 queries