Scope of DOM | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Scope of DOM

Giving commands via console in Chrome, the value of x= document.body.getElementByTag("span") sometimes returns the all span elements of the actual html, othertimes returns the all span elements inside of a iframe in the actual html. How to control the scope of DOM?

23rd Nov 2018, 12:04 PM
Daniel Bandeira
Daniel Bandeira - avatar
10 Answers
+ 7
I'm not too sure whether this will work with the spans in the iframe, but perhaps you can try: x = document.querySelectorAll("body span") for all the spans within body scope. x = document.querySelectorAll("#container span") for all the spans within scope of a container having Id #container. x = document.querySelectorAll(".container span") for all the spans within scope of container(s) having class .container. Reference: https://www.w3schools.com/jsref/met_document_queryselectorall.asp Hth, cmiiw
23rd Nov 2018, 4:16 PM
Ipang
+ 4
Well, if you try on an iframe which loads a different document from other domain I suppose it should happen (wild guess). Post back here if you find a way to do it cross domain will you?
24th Nov 2018, 3:00 AM
Ipang
+ 3
say me if it does work ("Span[0]") indicates first span in Dom like wise 1,2,3...
23rd Nov 2018, 12:51 PM
UwU Discover Me
+ 1
Can you put your codes here for examining?
23rd Nov 2018, 1:42 PM
Gordon
Gordon - avatar
+ 1
I think you can add an id to the span you want to do something, and use document.getElementById to get the span element DOM
23rd Nov 2018, 1:56 PM
Gordon
Gordon - avatar
+ 1
Welcome, though from your answer I think I have not helped you. About how to post your problem codes I think you can post copy of your codes So when the x is in its case 1, you save a copy to freeze the status Then when x is in case 2, you save another copy Then post both codes here.
23rd Nov 2018, 2:10 PM
Gordon
Gordon - avatar
+ 1
I got problems because Cross-Domain prohibition. Anyway, was a good try
23rd Nov 2018, 7:36 PM
Daniel Bandeira
Daniel Bandeira - avatar
+ 1
but...I have really edited the page inside the iframe of another domain. ( I have a picture to prove it lolol). I will give you the news. Thank you again!
24th Nov 2018, 8:15 AM
Daniel Bandeira
Daniel Bandeira - avatar
0
in fact, I don't know how to change the scope to do test and give you a answer. But the x value defined above changes as I atempt to get and change values inside the iframe
23rd Nov 2018, 1:51 PM
Daniel Bandeira
Daniel Bandeira - avatar
0
I want to get it by its html/innertext... But I need to get him first inside the iframe... to me this is very "fuzzy"... anyway, thank you for your helping
23rd Nov 2018, 2:07 PM
Daniel Bandeira
Daniel Bandeira - avatar