skip to Main Content

I have a div that’s loaded dynamically

<div id="box"></div>

I’m trying to grab the div using jQuery and add text to it

$("#box").text("Hello World");

But I’m unable to do so. A delegation issue I assume. Any solutions?

2

Answers


  1. Chosen as BEST ANSWER

    Update Solution

    I was loading the <div> using the jQuery load() method. I decided to place the $("#box").text("Hello World"); in the callback function and it worked


  2. The example below works like a charm

    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" rel="stylesheet">
        <script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
        <script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
        <link href="design.css" rel="stylesheet">
    </head>
    <body>
        <div id="box"></div>
        <script>
            $("#box").text("Hello World");
        </script>
    </body>
    </html>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search