Thursday, 22 August 2013

JQuery get id from $(this) not working, but getting another attribute does

JQuery get id from $(this) not working, but getting another attribute does


I've searched the web and tried every proposed method I can find, but
nothing seems right for my case.
The problem is this: I need to get the id of the checked element so I know
which element to change/manipulate later.
My problem is most curious because I do a $(this).attr("tag"); and get the
right value returned for later use.
But then I try $(this).attr("id") and when I alert it, it's just an empty
alert. I have also tried alternate methods.
I've done this.id and var $this = $(this); $this.id/$this.attr("id");
I cannot for the life of me figure out why it works on one attribute, but
not another when they're both within the same method, right after one
another.
$('.jq-toggleExternal').click(function () {
var refNo = $(this).attr("tag");
var checked = $(this).find(':checkbox').is(':checked');
var controls = getReferenceContolsByRefNo(refNo);
var id = $(this).attr('id');
alert(id);
<asp:CheckBox ID="r_chkRef1External" runat="server"
CssClass="jq-toggleExternal"
tag="1" Text="Ekstern" onClick="changed(this)" />
I've also tried changing the ID to id, but same result and if I change the
attr search to attr("ID") it says undefined.
Any possible solutions/alternative ways to get the id is highly appreciated.

No comments:

Post a Comment