0

I tried all solutions available online but unable to achieve the result! I want to access element inside iframe of my index.html page

index.html

//jquery-3.6.0 cdn
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>

//iframe
<iframe id="myIFrame" src='test/button.html' width="80%" height="600"></iframe>

<script>
    $(document).ready(function () {
        //change button color inside iframe to red when clicked
        $("#iviFrame1").on("load", function () {
            $("#iviFrame1").contents().find("button").on("click", function () {
                $("#iviFrame1").contents().find("button").css("background-color", "red");
            });
        });
    });
</script>

I also tried:

$('#quizIFrame').contents().find('body')

Also tried by replacing load with ready and ready with load.. but not working!!

isherwood
  • 58,414
  • 16
  • 114
  • 157
Sourav Dutt
  • 70
  • 1
  • 8
  • Doesn't work because you can't do that. – Leandro Bardelli Nov 19 '21 at 19:08
  • Protip: Use `$(this)` inside your load function (twice) to avoid multiple seek operations and to slim down your code. – isherwood Nov 19 '21 at 19:10
  • @LeandroBardelli, But what about answers on below link? https://stackoverflow.com/questions/1796619/how-to-access-the-content-of-an-iframe-with-jquery – Sourav Dutt Nov 19 '21 at 19:13
  • @SouravDutt my apologies. What about close as duplicated? – Leandro Bardelli Nov 19 '21 at 20:00
  • Does this answer your question? [How to access the content of an iframe with jQuery?](https://stackoverflow.com/questions/1796619/how-to-access-the-content-of-an-iframe-with-jquery) – Leandro Bardelli Nov 19 '21 at 20:00
  • @LeandroBardelli I already told, no solution is working for me! Don't know why!! I've seen a working solution on w3school and also on jQuery official documentation but when i use that solution it does not work for me. – Sourav Dutt Nov 20 '21 at 03:53
  • @SouravDutt sorry, didn't realize you were the OP. Last night was very sleepy. You have to put the content of the iframe. – Leandro Bardelli Nov 20 '21 at 13:43

0 Answers0