Reputation: 342
I created 2 radio buttons
<input type="radio" name="ApprovalGroup" runat="server" id="ApprovedOnly" value="true" />Approved
<input type="radio" name="ApprovalGroup" runat="server" id="UnapprovedOnly" value="false" />Unapproved
And was able to access them from js with $("input[name=ApprovalGroup]:checked").val()
But then I needed to add runat="server"
so I could access the radio button in the code behind.
The problem I have is the radio button name is being changed because of the content place holder. I'm using ClientIDMode="Static"
but it only protects the id value, not the name. The radio button is rendered as
<input value="true" name="ctl00$cphContent$ApprovalGroup" type="radio" id="ApprovedOnly" />Approved
<input value="false" name="ctl00$cphContent$ApprovalGroup" type="radio" id="UnapprovedOnly" />Unapproved
Is it possible to prevent the name from changing?
Upvotes: 2
Views: 530
Reputation: 191729
You could use the $=
selector, which selects attributes whose values end with the given substring:
$("input[name$=ApprovalGroup]:checked")
Upvotes: 5
Reputation: 4179
$("input[name*=ApprovalGroup]:checked")
is a good solution but if there is another input that contains these words, then it can be occured a conflict. So safer solution is to use your own attribute. such as;
<input type="radio" name="ApprovalGroup" data-group="group1" runat="server" id="ApprovedOnly" value="true" />Approved
<input type="radio" name="ApprovalGroup" data-group="group1" runat="server" id="UnapprovedOnly" value="false" />Unapproved
then use filter below;
$("input[data-group='group1']:checked")
Upvotes: 1
Reputation: 114367
.NET wants the name for its own purposes, but that doesn't stop you for using a CSS class name for your own.
That way you can use $('.classname').val()
. You can use individual class names for fields, or share them to make groups.
Upvotes: 3