1

How to access a element of a frame from other frame. For Ex:

Main.html:

<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    </head> 
    <frameset rows="33%,33%,*">
        <frame class="fra" src="frame1.html"/>
        <frame class="fra" src="frame2.html"/>
    </frameset>
</html>

frame1.html:

<html>
    <HEAD>
        <meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS" />
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    </HEAD>
    <body>
        <b><p id="para"> This is frame one.html </p></b>
    </body>
</html> 

frame2.html:

<html>
    <HEAD>
        <meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS" />
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    </HEAD>
    <body>
        <b><p id="para"> This is frame two.html </p></b>
        <button id="but"> Get data </button>
        <script>
            $(document).ready(function(){
                $("#but").click(function(){
                    alert(window.frames[0].document.getElementById('para'));
                });
            });
        </script>
    </body>
</html>

Once the button is clicked from frame2 then I need to get the data of "para" id element which is present in frame1. So, I tried to access the element as showed below. But it is not worked.

    window.frames[0].document.getElementById('para')

It shows the error as:

Uncaught TypeError: Cannot read property 'document' of undefined

So, window.frames[0] itself undefined .Can any one help me to solve this?

2

1 Answer 1

2

You should put id on your iframes, like "iframe1" and "iframe2".

    <frame class="fra" src="frame1.html" id="frame1" />
    <frame class="fra" src="frame2.html" id="frame2" />

Then:

$(window.parent.document).find("#iframe1").contents().find("#para")

should give you access from iframe2 to the element with id "para" in frame one.

$(window.parent.document) will allow you to return from iframe2 to the main document, then find iframe1, then contents() will allow you to go inside iframe1 where you'll be able to find the "#para" element.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.