0

I want to get label of element as string and alert that string in jquery. I wrote code below but i get [object][Object] value. I tried html() and text() too but it didnt work.

 function seatObject(id, label, status, token){
        this.id = id;
        this.label = label; 
        this.status = status;
        this.token = token;
    }

    for (var x = 0; x < 5; x++) {            
      var temp = new seatObject("#" + x, "label" + x, "available", "");
      seats[x] = temp;                        
      $("#" + x).click(function () {
        currentSeat = $(this).attr("id");
        var label = $("label[for="+$(this).attr('id')+"]");
        alert(label);   
      });
    } 

Also this is my tag;

<rect style="fill: #888888;" id="0" width="35.823246" height="35.823246" x="299.87155" y="65.999405" class="seatObj" label="A24"></rect>
ebruszl
  • 469
  • 4
  • 11
  • 27
  • 3
    var label = $("label"); alert(label.text()); This works for me http://fiddle.jshell.net/kU7Pb/ – Mina Oct 06 '13 at 08:01
  • http://stackoverflow.com/questions/4844594/jquery-select-the-associated-label-element-of-a-input-field – Matt Bodily Oct 06 '13 at 20:12
  • I tried it but i got [object][Object] value – ebruszl Oct 07 '13 at 11:24
  • what does your `seatObject` function do ? can you post the code ? – LeGEC Oct 07 '13 at 11:42
  • function seatObject(id, label, status, token){ this.id = id; this.label = label; this.status = status; this.token = token; } – ebruszl Oct 07 '13 at 11:48
  • Where do you actually add the label node to your DOM document ? – LeGEC Oct 07 '13 at 12:13
  • var temp = new seatObject("#" + x, "label" + x, "available", ""); In this line I add the label node. When i write these codes; currentSeat = $(this).attr("id"); I am getting id. After that, I want to show user that you clicked that seat with label of seat – ebruszl Oct 07 '13 at 12:25

2 Answers2

0

If you are using asp label use this

var labelText  = $('#<%= label.ClientID %>').val();

Or If you are using html label use this

var labelText = $('#label').val();

This should work for u..

Jidheesh Rajan
  • 4,744
  • 4
  • 23
  • 29
0

Give class to your label since you are using server-side objects.

<label runat="server" id="myLabel" CssClass="myLabelClass"></label>

alert($(".myLabelClass").text());

should work.

Pabuc
  • 5,528
  • 7
  • 37
  • 52