0

Hi, I'm trying to get the alert method to show the value c8 but it just says undefined.

I'm new to html and jquery; it's simple but I just can't see it.

<!DOCTYPE html>
<html>
  <head>
    <script type="text/javascript" src="jquery/jquery-1.7.1.js"></script>
    <script>
      $(document).ready(function(){
        $("button").click(function(){
      //$("#div1").load("hs.php");
      //$("#div1").load("http://127.0.0.1:81/tenHsServer/tenHsServer.aspx?t=ab&f=DeviceStatus&d=A1");
        $("#div1").load("http://127.0.0.1:81/tenHsServer/tenHsServer.aspx?t=ab&f=ToggleDevice&d=c8");
        alert($(this).prev().attr(".class"));
       });
      });
    </script>
  </head>
  <body>
    <div id="div1"><h2>Let jQuery AJAX Change This Text</h2></div>
    <span class='c8'><button>Get External Content</button></span>
  </body>
</html>
ekremkaraca
  • 1,453
  • 2
  • 18
  • 37
user2125298
  • 11
  • 1
  • 1

5 Answers5

1

Because prev() is for sibblings only. your button is child of that div.

You should go:

alert($(this).parent().attr("class"));
monxas
  • 2,475
  • 2
  • 20
  • 36
1

The button has no siblings .prev() is used to select the previous sibling, the element you want to target is its parent therefore use .parent() to select it. Also you only prepend the class with a dot (.) when it is part of a selector.

alert($(this).parent().attr("class"));
Musa
  • 96,336
  • 17
  • 118
  • 137
0

You don't need the dot before attribute names:

alert($(this).prev().attr("class"));
SeinopSys
  • 8,787
  • 10
  • 62
  • 110
0

While in jQuery the . introduces a class selector, in this case you just want an attribute called "class".

Remove the . in .class:

alert($(this).prev().attr("class"));
Eric J.
  • 147,927
  • 63
  • 340
  • 553
0

You should use .attr("class") instead (without the .)

DarkAjax
  • 15,955
  • 11
  • 53
  • 65