Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

div elements not updating with javascript

Can any of you see why the div (#welcome) is not being updated in the below code?

function show_logged_in(success)
{

    var is_logged_in = success;

    var dataString = {"reg":invalid, "login":invalid,"is_logged_in":is_logged_in};

    $.ajax({
        type:"POST",
        url:"PHP/class.ajax.php",
        data:dataString,
        dataType:'JSON',
        success: function(username) {
            alert("User is shown");
            user = username;
            change_div();
        },
        error: function() {
            alert("ERROR in show_logged_in")
        }
    });

function change_div(){
    $('#welcome').style.display = 'block';
    $('#welcome').innerHTML = "Welcome" + user + "to SIK";
    }

}

The response from the ajax called is simply grabbing the username from the session variable. and it is returning correctly.

And when it returns i would like the div to show up and say welcome. But for some reason the div is not being updated.

Here is the html:

<div id="welcome" style="display:none; postion:absolute; top:0; float:right;">...</div>

like image 817
Tom Burman Avatar asked Mar 20 '13 11:03

Tom Burman


2 Answers

You can't do

$('#welcome').style.display = 'block';
$('#welcome').innerHTML = "Welcome" + user + "to SIK";

with jquery. This is because with $('#welcome') you are grabbing the jQuery object, not the DOM element.

To do this with jQuery:

$('#welcome').html('Welcome' + user + 'to SIK').show(); // Brought up in comments

$('#welcome').show(); // Old
$('#welcome').html('Welcome' + user + 'to SIK'); // Old

Or if you really want to grab the DOM Element:

$('#welcome')[0].style.display = 'block';
$('#welcome')[0].innerHTML = "Welcome" + user + "to SIK";
like image 69
Jeff Shaver Avatar answered Oct 30 '22 03:10

Jeff Shaver


@Jeff Shaver is right but Pass user as argument change_div(user);

then

function change_div(user){
       $('#welcome').show();
       $('#welcome').html('Welcome' + user + 'to SIK');
}
like image 38
Devang Rathod Avatar answered Oct 30 '22 02:10

Devang Rathod