JQUERY: search by control id

My aspx page currently contains

<input type="text" name="openid_username" />
<input type="text" name="openid_identifier" />

but now I would like to replace them

<asp:TextBox ID="openid_username" runat="server"></asp:TextBox>
<asp:TextBox ID="openid_identifier" runat="server"></asp:TextBox>

so how do I change the following jquery to display input fields to replace text fields?

  var $usr = $this.find('input[name=openid_username]');
  var $id = $this.find('input[name=openid_identifier]');
+5
source share
6 answers

I would use the "ends with" parameter in the attribute selector in case the name is controlled by a control located in the container. Pay attention to $=instead =.

var $usr = $this.find('input[name$=openid_username]');
var $id = $this.find('input[name$=openid_identifier]');
+8
source

If you need the exact identifier of your controls, you can do this:

var $usr = $this.find('#<%= openid_username.ClientID %>');
var $id = $this.find('#<%= openid_identifier.ClientID %>');

Of course, this assumes your JS is not in an external file.

+3
source
 var $usr=$("#openid_username");

 var $usr = $('[id*=openid_username]');
+2

, ID ID ( <input type="text" id="openid_username"> aspx html, :

var $usr = $("#openid_username");
+1
var $usr = $this.find('#openid_username');
var $id = $this.find('#openid_identifier');
0
source

I would do the following:

var $usr = $("input[id$='openid_username']");
var $id = $("input[id$='openid_identifier']");
0
source

All Articles