Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,
I want to check an element that exists in a parent window.

CODE

ASP.NET
<asp:TextBox ID="txtPortfolio" runat="server" ReadOnly="true" CssClass="input" MaxLength="150" name="txtPortfolio" Width="200px" />


JavaScript
var folio = document.querySelector("txtfolio")


It throws null. So how can I access parent window element? Pls help

What I have tried:

I tried this

JavaScript
if (document.body.contains(document.getElementById('txtPortfolio'))) {
                            alert('Element exists!');
                        } else {
                            alert('Element does not exist!');
                        }
Posted
Updated 11-Jan-22 1:13am
v2

1 solution

You have two options really:

Window.top - Web APIs | MDN[^]
Window.parent - Web APIs | MDN[^]

window.top refers to the absolute parent. This can be used from JS which has been nested multiple times to get the uppermost parent.

window.parent refers to the direct parent of the current page. If nested multiple times then it'd be the one directly above, not necessarily equal to window.top

You can use the query selector as usual: window.top.document.querySelector()

Also, to follow up on what you posted, what is "txtfolio"? You used querySelector() to find "txtfolio" but that isn't a valid HTML element. If that's the name of the element then you should instead use getElementsByName()[^]
 
Share this answer
 
v2
Comments
Member 14362033 11-Jan-22 7:10am    
Hello Chris,
"txtfolio" is the id of the textbox where I want to assign my child popup page value.I will try your solutions. Thanks
Member 14362033 17-Jan-22 3:36am    
Hello Chris,
I wrote
var folio = window.parent.document.getElementsByName("txtfolio"); alert(folio);

It throws object nodeList alert. What does it mean?
Member 14362033 17-Jan-22 6:33am    
Thanks, Chris this solved my problem. The element exits but how can I assign its value now?
Chris Copeland 17-Jan-22 8:04am    
The getElementsByName produces a nodeList which is one or more elements. In order to change the value you'll need to get the first element in the list, then update the value directly, like so:

const elements = window.parent.document.getElementsByName("txtfolio");
const target = elements[0]; // This is the first match
target.value = "Hello world!";
Member 14362033 17-Jan-22 9:32am    
Chris, when I do this
alert(target)
Cannot set properties of undefined (setting 'value') I am getting this error. I get undefined in alert box. Can you please help me?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900