0

Trying to learn Ajax - jQuery through simple code samples I have this two html files as: index.html and source.html.the inex.html is as:

enter code here
<!DOCTYPE html>
 <html>
 <head>
 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
 <script>
  $(document).ready(function() {
   $('button').click(function(){
   $.get('source.html', function(data) {
   $('.result').html(data);
   });
  });
 });
 </script>
 <title>Main Page</title>
 </head>
 <body>
 <button>Load Page</button>
 <div class="result"></div>
 </body>
 </html>

and the source.html as:

enter code here
<!DOCTYPE html>
<html>
<head>
<title>Source Page</title>
</head>
<body>
<div class="test1">Hello Ajax!</div>
<div class="test2"> Again Hello Ajax!</div>
<div class="test1">WoW Ajax!</div>
 <p> The html() method sets or returns the content(innerHTML) of the selected... </p>
 </body>
</html>

now my question is how I can grab specific elements instead of getting all elements in the index page? For example , how I can filter the request object to just get all div with class "test1" or only

from the page source.I also do not really understand what does "data" parameter means in

enter code here
$.get('source.html', function(data) {
$('.result').html(data);

can you please let me know what does it mean?

data from Console:

<!DOCTYPE html>
<html>
<head>
<title>Main Page</title>
</head>
<body>
<div class="test">Hello world!</div>
<div class="test1">Hello Ajax!</div>
<div class="test2"> Again Hello Ajax!</div>
<div class="test1">WoW Ajax!</div>
<p> The html() method sets or returns the content (innerHTML) of the selected   elements.When this method is used to return content, it returns the content of the FIRST   matched element. When this method is used to set content, it overwrites the content of ALL matched elements.</p>
 </body>
 </html>
1
  • but it is working on my machine Commented Nov 15, 2012 at 8:41

2 Answers 2

1

After getting the response of your ajax call, you can find elements in the following way:

$.get('source.html', function(data) {
   test1_elements = $(data).find('.test1')
});

test1_elements will now contains all the elements with class test1 from your source.html.

Sign up to request clarification or add additional context in comments.

6 Comments

Hi HungryCoder thanks for your comment but this is not returning anything to page I modify the code to code $.get('source.html', function(data) { test1_elements = $(data).find('.test1') $('.result').html(test1_elements); });code but it doesnt retrun anything to index page
check in console to see what you actually got. ` test1_elements = $(data).find('.test1') ; console.log(test1_elements)`
Ok I am just getting [] @ line 11.
well, then check whole data using console.log(data)
are you getting html that matches to source.html? post it somewhere.
|
0

You can grab specific elements by their id or class:

var $byids= $('#id');
var $byclass= $('.class');

The data parameter in the function contains the entire response data, that is what source.html shows.

You can use

var $div= $(data).find('#divid'); 

to select stuff from the data.

Updated: You have 2 test1 elements. The following will get the html of each, and stitch it together. The last line displays the summary in all elements with class "test" in the document.

$.get('source.html', function(data) {
    var $test1_elements = $(data).find('.test1');
    var summary= '';
    $test1_elements.each({function(){ summary+= $(this).html(); });
    $('.test').html(summary);
});

1 Comment

hi adder but how I can use var $byids= $('#id'); inside the $.get()?

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.