Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.html() returns undefined

I'm new to JQuery and am having trouble getting the content of a div block.

  <div class="update_status">
    Updated successfully
  </div>

And when the following script runs:

$(document).ready(function () {
  $(function () {
    var $status_div = $("#update_status");
    if ($status_div) {
      alert("1");
      alert($status_div.html());    
      alert("2");
    } else {
      alert("undefined");
    }
  });
});

I receive three alerts, "1", "undefined", and "2". I expected to get "Updated Successfully". Any ideas on what I did wrong?

like image 330
Eytan Avatar asked Nov 30 '22 23:11

Eytan


1 Answers

# is used to find elements by id. You are using a class so you need .

$(".update_status");

http://api.jquery.com/category/selectors/

like image 189
James Montagne Avatar answered Jan 03 '23 13:01

James Montagne