0

I am trying to use jquery on an Asp.net control. In asp.net id get generated from server. so I am trying to write a selector to use jquery.

  $(document).ready(function() {
        $("#ctl00$cphBody$fv$txtDescription").val('blah');
        });

I need if the id contains 'txtDescription', I need to change the value of that textbox. Please suggest a selector for that

3 Answers 3

1

A much better choice would be to assign a css class and then use that to identify the elements. Something like:

$(function(){
  $(".someClass").val('blah'); 
});

If that is not feasible for your current scenario, try the code below. It checks for the id's ending with txtDescription as this is what you would need):

$(function(){
  $("[id$='txtDescription']").val('blah'); 
});

If you really need contains (i.e. anywhere in the id) then try:

$(function(){
  $("[id*='txtDescription']").val('blah'); 
});
Sign up to request clarification or add additional context in comments.

Comments

1

please dont use

  $("#ctl00$cphBody$fv$txtDescription").val('blah');

use

$("#<%= txtDescription.ClientID%>")  instead

thanks.

Comments

0

If you're on ASP.NET 4.0, consider using the new ClientID modes to avoid having to mess with the nasty long ClientIDs in Javascript code. This will be more maintainable than using "contains" operations.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.