.NET Daily

How to search a control with jQuery knowing only a part of the control name

Introduction

Darius

Darius

Darius Dumitrescu is a creative Senior CMS Consultant with in depth .NET knowledge, focused on Web Development and Architecture Design.


LATEST POSTS

How to Deliver Accurate Project Status Reports 21st April, 2018

JIRA Workflow for Web Development Example: Simple and Effective 17th April, 2018

jQuery

How to search a control with jQuery knowing only a part of the control name

Posted on .

ASP.NET Web Forms require unique IDs for server controls in the page so if you want to search a control by a predictable name from Javascript, its pretty hard, well not hard but unflexible. Either give the full name you find in View Source and you pay attention if the ID will change during builds or you put server tags “<%= ControlName %>”, which it will get the ID dynamically, no matter what will happen in that page.

But what if you create a .js file to keep your page clean and with fewer lines of code? The server tags will not work and providing the full name from View Source is not a good ideea. You can however use jQuery for this. Ex:

var btnAddID = $('input[name*="btnAddNewItem"]').attr("name");

The sign “*=” tells jQuery to search the text in quotes in the interior of the controls ID. It works just like the “LIKE” operator from SQL 🙂

Cool right? Now you can export those big chunks of code from pages  into .js files

Darius

Darius

Darius Dumitrescu is a creative Senior CMS Consultant with in depth .NET knowledge, focused on Web Development and Architecture Design.

There are no comments.

View Comments (0) ...
Navigation

Privacy Preference Center