tags:

views:

57

answers:

3

I have appended a div with an id div1 and immediately, the next line aims at appending contents to this div. Its not selecting. Whats going wrong?

$('body').append("<div id ='backFade' style='background-color: Black; left: 0; top: 0; width: 100%; height: 100%;min-height: 768px; position: absolute; -moz-opacity: 0.7; filter: alpha(opacity=70);opacity: 0.7;'></div>");
    $('body').append("<div id='popup' style='background-color:white;display:block; border: solid 3px #A9D0F5;top: 45%;left:50%; margin-top:-75.5px; margin-left:-203px;width: 500px; height: 200px; z-index: 100; position: fixed;'></div>");
    $('#popup').append("hi");

"hi" not coming up :(

A: 

Try this:

// Create and append div1.
var div1 = $('<div id="div1" />');
$('.some-element').append(div1);

// Append contents.
div1.append('<div id="div2" />');
// Etc.

Edit: Looking at your code, I think what you want is to use the text function:

$('#popup').text('hi');

Edit: OK, try this code snippet; does it work?

<html>
    <head>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;
        <script type="text/javascript">
        $(function()
        {
            $('body').append("<div id ='backFade' style='background-color: Black; left: 0; top: 0; width: 100%; height: 100%;min-height: 768px; position: absolute; -moz-opacity: 0.7; filter: alpha(opacity=70);opacity: 0.7;'></div>");
            $('body').append("<div id='popup' style='background-color:white;display:block; border: solid 3px #A9D0F5;top: 45%;left:50%; margin-top:-75.5px; margin-left:-203px;width: 500px; height: 200px; z-index: 100; position: fixed;'></div>");
            $('#popup').append("hi");
        });
        </script>
    </head>
    <body>
    </body>
</html>
Will Vousden
no yaar, what i need is append . That was just an example. Even text is not working
Ajay
Well there's the obvious question of whether you've got your code inside a document ready event handler?
Will Vousden
the function which does all these in called on onload of body
Ajay
there is nothing different in this except that jQuery is accessed from google.
Ajay
If that doesn't work then I'm not sure. Have you verified that JavaScript is actually being executed? Try putting some `alert('foo')` calls in there.
Will Vousden
+1  A: 
rahul
tried both, in vain!
Ajay
+2  A: 

Why not just append it directly after creation?

$('body').append (
    $('<div id="popup"></div>')
        .css ({
            'backgroundColor': 'white',
            'display': 'block',
            'borderr': 'solid 3px #a9d0f5',
            'top': '45%',
            'left': '50%',
            'marginTop': '-75.5px',
            'marginLeft': '-203px',
            'width': '500px',
            'height': '200px',
            'zIndex': '100',
            'position': 'fixed'
        })
        .append("hi")
);
K Prime
borderr should be border.
Jimmie Lin
this one's working :)
Ajay