By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,932 Members | 1,291 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,932 IT Pros & Developers. It's quick & easy.

getting document elements by regular expressions

P: 1
Hi All,

I need to fetch a document-element but i do not know the full Id of the element.
I only know part of the id of the element. Is it possible to get something like "Give me the element which has id starting with XXXXX".

Say we have 3 elements
1)<input type="text" id="My_name_is_ABC value="textboxval">
2)<input type="text" id="My_name_is_BEF value="textboxval">
3)<input type="text" id="My_name_is_YEX value="textboxval">

now , in my script i have only "My_name_is_A" and i want to get a reference of an object whose id starts with "My_name_is_A".
Is it possible to do so ?
Can i use something like
var regularExp = new RegExp("My_name_is_A");
document.getElementById(regularExp);

Of course the above does not work :(
Jun 27 '07 #1
Share this Question
Share on Google+
2 Replies


100+
P: 428
id="My_name_is_ABC value="textboxval">

It will never work unless you close the quotes for the id value.

id="My_name_is_ABC" value="textboxval">
Jun 27 '07 #2

100+
P: 210
Hi All,

I need to fetch a document-element but i do not know the full Id of the element.
I only know part of the id of the element. Is it possible to get something like "Give me the element which has id starting with XXXXX".
There could be more than one element with an id starting with that prefix. This will return a reference to the first one found:
Expand|Select|Wrap|Line Numbers
  1. function getFirstElementWithIDPrefix( prefix )
  2. {
  3.  var allElems=document.getElementsByTagName('*');
  4.  
  5.  for( var i=0; i<allElems.length && allElems[i].id.indexOf(prefix); i++)
  6.  ;
  7.  
  8.  return (i==allElems.length) ? null : allElems[i];
  9. }
Jun 27 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.