devdarsh
devdarsh

Reputation: 95

get content of a div from a variable using jQuery

i am building a php form with ajax and i am submitting the form variables to the same page where the form resides. my validating code looks like

if($valid){
    echo "<div id='retmsg'>Your message has been submitted successfully</div>";
}
else {
    echo "<div id='retmsg'>An error occured!</div>";
}

My ajax code

function form_submit() {
 $('#response').html('Loading...').fadeIn();
$.ajax({
    type: "POST",
url:  "<?php $_SERVER['PHP_SELF']; ?>",
data: "title=" + $('#title').val() + "&content=" + $('#contents').val(),
success: function(html){
var message = $("#retmsg").html(html);
alert(message);
  }

}); 
}

i need to alert the contents of div id "retmsg". But the alert shows me like "[Object Object]". Please help me...

Upvotes: 0

Views: 8248

Answers (4)

devdarsh
devdarsh

Reputation: 95

Thanks for all gentle mens who shared my issue. As i mentioned in my description, as i submitted the form to the same page, the page loads again with all components i used such as jQuery library and js files i.e the whole document. That's the problem. I simply used if(post){ } else { form validation } for the whole html content and got it solved.

Upvotes: 0

Alex Pacurar
Alex Pacurar

Reputation: 5861

take a look at jquery`s html(). if you want to retrieve the html content of a certain element you need to use html() function with no arguments.

so from this:

var message = $("#retmsg").html(html);

to this:

var message = $("#retmsg").html();    

The "[Object Object]" that the

$("#retmsg").html(html);

returns is actually a jquery object. You can find more on jquery chain

Upvotes: 0

Davide Ungari
Davide Ungari

Reputation: 1960

Try

 alert(message.html())

Upvotes: 0

Salil
Salil

Reputation: 47482

Check

var message = $("#retmsg").value;

Upvotes: 1

Related Questions