Question

I already used document.getElementById, but it does not work any more. I want to get the value of 0,01€ but I can not. I want to save it in my NSString *price but how. The HTML code is

<tr class="price">
<td>0,01</td>
<td>EUR</td>

My idea was

NSString *price = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('price.td').innerText;"];
Was it helpful?

Solution

The tagName in this case is 'td' not 'price.td', but if you run document.getElementsByTagName('id') you will get all the table cells in the page which is probably not what you want.

If the page has jQuery loaded in it you can use jQuery('.price td:first').text() to get the price.

If it doesn't have jQuery but you have control of the page you can add a class to it (<td class="my-price">12.44</td>) and get it with document.getElementsByClassName('my-price')[0].innerHTML.

If you don't have control of the page and it doesn't have jQuery you will have to find the 'price' row and then get its 1-st cell by using document.getElementsByClassName('my-price')[0].childNodes[0].innerHTML.

OTHER TIPS

Also, a cleaner and more flexible approach is to use the Selectors API:

http://www.w3.org/TR/selectors-api/

document.querySelector('.price td:first').innerText
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top