Reputation: 9283
<table cellpadding="1" cellspacing="1" id="member"><thead>
<tr>
<th> </th>
<th>Gracz</th>
<th>Ludność</th>
<th>Osady</th><th> </th></tr></thead><tbody><tr>
<td class="ra">1.</td><td class="pla"><a href="spieler.php?uid=5473">rafal12</a></td><td class="hab">8801</td>
<td class="vil">12</td><td class="on"><img class="online2" src="img/x.gif" title="Logował(a) się w ciągu ostatnich 24 godzin" alt="Logował(a) się w ciągu ostatnich 24 godzin" /></td></tr><tr>
<td class="ra">2.</td><td class="pla"><a href="spieler.php?uid=3136">rspboss</a></td><td class="hab">7131</td>
<td class="vil">11</td><td class="on"><img class="online2" src="img/x.gif" title="Logował(a) się w ciągu ostatnich 24 godzin" alt="Logował(a) się w ciągu ostatnich 24 godzin" /></td></tr><tr>
<td class="ra">57.</td><td class="pla"><a href="spieler.php?uid=762">zxcv</a></td><td class="hab">1670</td>
<td class="vil">4</td><td class="on"><img class="online1" src="img/x.gif" title="teraz on-line" alt="teraz on-line" /></td></tr><tr>
.... and so on...
That's sample of html table that I want to collect data from. What I want to retrive are values inside img tags inside this table and make list of them. How can I get that value?
How can I say if img class
was online1
or online2
?
Any help is welcome
PS: it would be great, if someone would be so nice, to guide me how to make list of them and put it into XMLHttpRequest
Upvotes: 0
Views: 486
Reputation: 10926
this script will grab all the classes for you of the image tags.. not really sure what you are looking for as far as the XMLHttpRequest but this should help you on your way
var imgTags = document.getElementsByTagName('img');
var results = [];
for(var i=0;i<imgTags.length;++i)
{
results.push(imgTags[i].className);
}
the results are stored in the array results;
Upvotes: 2
Reputation: 243
If I understood correctly you want to retrieve the values of different attributes from img elements in the page. In this case you can use the getAttribute method (see http://reference.sitepoint.com/javascript/Element/getAttribute) which gets the value of a certain attribute from an element. To get the img elements you can use the getElementsByTagName method (see http://reference.sitepoint.com/javascript/Document/getElementsByTagName). Using the two methods you could iterate through all the img elments and compare their classes:
if(imageElement.className == 'online1')
{
var value = imageElement.getAttribute('src'); //This will retrun the value for the source of the image
}
// The same for online2 class
Upvotes: 0
Reputation: 522
//Find all the images that are inside the tbody of the table with the id member
oImages = document.getElementById("member").getElementsByTagName("tbody")[0].getElementsByTagName("img");
//Create an array that will contain refrences for all the img tags
aImages = [];
//go through all of the images, check their class, and append them to
//the array if the class match
for(i-0; i<oImages.length; i++){
if(oImages[i].className == "online1") aImages.push(oImages[i]);
}
Upvotes: 3
Reputation: 76208
You can check for class names by saying:
document.getElementById([id]).className
But since you don't have id on images, it becomes little difficult. Doing it pure JS way is to get the table element and loop through its children until you find the img tag.
I suggest adding an id to img tags or use a library like jQuery to make it easy.
Upvotes: 2