0

I have a page that has a table with ID as below

<table id="T1">
<tbody>
<tr>
<td>Qassas</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>

<tr>
<td>test</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
</tbody>

</table>

Via Page URL (I think I need to use something like XMLHttpRequest),

  • How can I get an element (Table) with its ID?

    • Then check if the qassas value exists in a table "T1" **in this page, retrieve the values of all tds beside the cell that contains the matched value qassas

P.S: the value is located in the first column

Any help would be fully appreciated

Mohamed
  • 806
  • 13
  • 30

1 Answers1

1

Not possible to get the HTML content of a page if you've not access to this page, and not written a script that return the content.

I support my answer with this Stackoverflow question: How to get html source code from external url

Otherwise, if want to check if the value Qassas exist inside the table T1 you need to use the jQuery selector :contains, here is a simple demo:

//This Script check if "Qassas" exist in the table or not
if( $('#T1 td:contains("Qassas")').length  > 0)
{
  alert("Qassas exist");
}else
{
  alert("Qassas doesn't exist");
}

//This script retrieve the values of the closest "td" tags

var tr=$('#T1 td:contains("Qassas")').parent();
var values=[];
tr.children().each(function(){
  values.push($(this).text());
});
console.log(values);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="T1">
    <tbody>
    <tr>
    <td>Qassas</td>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    </tr>
    
    <tr>
    <td>test</td>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
    </tr>
    </tbody>
    
    </table>
Mehdi Bouzidi
  • 1,937
  • 3
  • 15
  • 31
  • Thank you, I got it what about the second request of search in table for a specific value, please post it to accept the answer – Mohamed Oct 18 '17 at 21:15
  • just click on the check mark, that you'll find at the left side of my answer. Good Luck – Mehdi Bouzidi Oct 18 '17 at 21:32
  • No Problem, I will. but I need to check if a value exists in a table "T1" get the td values besides! I mentioned this in my question, could you help me for this?! – Mohamed Oct 19 '17 at 06:32
  • @M.Qassas I edited my answer to show you how to check if `Qassas` exist or not – Mehdi Bouzidi Oct 19 '17 at 07:41
  • Thank you for your help, now I just need to get the value of all td beside the cell that contains qassas instead of show alert message :) could you help! – Mohamed Oct 19 '17 at 08:04
  • @M.Qassas by values you mean the text 1,2,3 ... ? What do you want with these values ? – Mehdi Bouzidi Oct 19 '17 at 08:09
  • yes bro, i need when find the matched value in a specific cell get the value of other cells in the same row, I am sorry for this confusion but i already mention this in my question. Thanks again for your helpful support :) – Mohamed Oct 19 '17 at 08:11
  • 1
    @M.Qassas sorry for that, I reedited my answer, you can get these values :) Good luck – Mehdi Bouzidi Oct 19 '17 at 08:19