PDA

View Full Version : Need this javascript code to work on this website



nullx
06-07-2015, 10:21 AM
I need to make my code work in this website
http://www.pcsoresult.com/swertres-lotto-results/

specifically I need only the results shown on the 11, 4 , 9 draws.
selected sources

<p data-iceapc="3" data-iceapw="10">11am <span class="IL_AD" id="IL_AD2">Draw</span>: <strong data-iceapw="1" class="winning-numbers">9-8-4</strong><br>
4pm Draw: <strong data-iceapw="1" class="winning-numbers">2-2-2</strong><br>
9pm Draw: <strong class="winning-numbers"></strong></p>

I use getElementsbyClassName to get the text because each draw has a "winning-numbers" class name.

<script>
var number = document.getElementsbyClassName("winning-numbers")
document.write(number)[0];
document.write(number)[1];
document.write(number)[2];

but I supposed it's not working

I need help here if someone can. I know it is easy task because we can get the value easily by using getElementsbyClassName method.

Beverleyh
06-07-2015, 01:07 PM
Are you just trying to grab the innerHTML as a string? i.e. The "9-8-4" part of the 11am line, "2-2-2" part of the 4pm line, and something similar for the 9pm line when a value is present?

getElementsByClassName() should work OK for modern browsers and IE9+, but watch your syntax! - there's a capital "B" in there. Also, document.getElementsByClassName() returns a nodeList, not an element, so if you want to get the innerHTML of an element this way, you need to do it like this;
document.getElementsByClassName("winning-numbers")[0].innerHTML;The example above gets the innerHTML of the first element with the "winning-numbers" class.

Im not sure what are you doing with the data once you have it because your page is crashing my browser (therefore I can't see from the source code), but if you want to insert the retrieved data into an existing element, try innerHTML() https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML

Note that document.write() is only useful while the page is loading and being generated for the first time, and when the script is run from the <head> of a web page. For dynamically updating the page after it has been generated, or for when a script is placed before the closing </body> tag, innerHTML() is probably a better choice.

So if you want to get the innerHTML of all 3 elements and then insert the values as innerHTML into 3 other elements, you could do it simply like this;

<body>

<p data-iceapc="3" data-iceapw="10">11am <span class="IL_AD" id="IL_AD2">Draw</span>: <strong data-iceapw="1" class="winning-numbers">9-8-4</strong><br>
4pm Draw: <strong data-iceapw="1" class="winning-numbers">2-2-2</strong><br>
9pm Draw: <strong class="winning-numbers">XXX</strong></p>

<div id="test1"></div>
<div id="test2"></div>
<div id="test3"></div>

<script>
document.getElementById('test1').innerHTML = document.getElementsByClassName("winning-numbers")[0].innerHTML;
document.getElementById('test2').innerHTML = document.getElementsByClassName("winning-numbers")[1].innerHTML;
document.getElementById('test3').innerHTML = document.getElementsByClassName("winning-numbers")[2].innerHTML;
</script>

</body>

nullx
06-08-2015, 01:04 AM
Yes I want to get the strings of the class name winning numbers

Thank you for the response regarding my question. I just want to ask one more time so there is no need to load the url or site which the classnames are came from or I could just directly implement the GetElementsbyClassName method without the url as the class reference.

Beverleyh
06-08-2015, 05:17 AM
Sorry, I'm not sure I understand the question.

Is pcsoresults.com not YOUR website?

You might want to read up on "same origin policy" which only permits scripts running in a browser to make requests to pages on the same domain.