tirenweb
tirenweb

Reputation: 31709

Trying to add an iframe using jQuery

I'm trying to add an iframe using jquery this way below, but when I click the link it doesn't happen anything.

<head>

    <script type="text/javascript"
            src='//ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.js'></script>

    <script type="text/javascript">

        function fun(){
            $('<iframe />');  // Create an iframe element
            $('<iframe />', {
                name: 'frame1',
                id: 'frame1',
                src: 'http://www.programmingfacts.com'
            }).appendTo('body');

    </script>

    </head>

    <body>

        <a href="#" onclick="fun()">clica</a>

    </body>

</html>

Upvotes: 4

Views: 32375

Answers (3)

Aaron Newton
Aaron Newton

Reputation: 2306

Try this:

<html>
    <head>
        <script type="text/javascript" src='http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.js'></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $(".submit").click(function()
                {
                    $('<iframe />');  // Create an iframe element
                    $('<iframe />', {
                        name: 'frame1',
                        id: 'frame1',
                        src: 'http://www.programmingfacts.com'
                    }).appendTo('body');
                });
            });
        </script>
    </head>
    <body>
        <a href="#" class="submit">clica</a>
    </body>
</html>

Upvotes: 8

Barrie Reader
Barrie Reader

Reputation: 10713

Change your function to:

function letsHaveFun(){
  $('body').append('<iframe src="http://www.programmingfacts.com" name="frame1" id="frame1"></iframe>');
}

That should work.

Upvotes: 4

benhowdle89
benhowdle89

Reputation: 37464

My guess is that you dont close off your function fun() closing bracket? }

Upvotes: 7

Related Questions