p...">

JQuery to get text attribute of checkbox

I add a check box to the page using the following statement:

<script language="C#" runat="server">
    protected void Page_Load ( object src, EventArgs e ) 
    {
        if (!IsPostBack)
        {
         CheckBox XChkBox = new CheckBox(); //instance of System.Web.UI.WebControls.CheckBox
         XChkBox.ID = "someId"
         XChkBox.Text = "someText"
         somePlaceHolder.Controls.Add(XChkBox);
        }
    }
</script>

I need to get the Text attribute of this checkbox when I click. I tried $(this).attr('Text');inside $('input[type=checkbox]').click(function(){});, but it returns undefined.

Where am I mistaken? Please suggest.

amuses

+5
source share
3 answers

ASP.NET displays the property of the Textserver control ASP:CheckBoxas an element labelimmediately after <input type="checkbox" />on the generated markup on the client looks something like this:

<input id="someId" type="checkbox" name="someId" />
<label for="someId"> someText </label>

You can get Text labelwith:

$("input:checkbox").click(function() {
  var $label = $(this).next('label');
  alert($label.text());
});
+10
source

CheckBox Text <label>. HTML-. jQuery, <label>.

, <label> . CheckBox checkBox1, HTML <label for="CheckBox1">, . , jQuery :

$('label[for="checkBox1"]').html()
+1

, , "".

:

<input id="chkFoo" type="checkbox" text="Check me, fool!" />

:

$("#chkFoo").attr("text")

,

<input id="chkFoo" type="checkbox" />Check me, fool!

, . , :

<input id="chkFoo" type="checkbox" /><span id="spnFoo">Check me, fool!</span>

$("#spnFoo").text()
0

All Articles