Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting element by ID when ID is approximate 1

Status
Not open for further replies.

developer155

Programmer
Jan 21, 2004
512
US
I have a div on the page and I only know part of its ID (id's are generated dynamically on server side for this program)
So lets say the partial name is bt1(the full name could be 123_btlf125). I need to use partial name I know to access that element by javascript function and hide it. What is the best way of doing it?

thanks
 
Take a look at my FAQ for implementing getElementsByClassName (faq216-6104)... This could easily be modified to do such a job. It gets every element on a page, and then loops over them, checking their class.

To modify it to work on IDs instead, the "if" condition in the last loop would need changing.

Assuming you renamed the "classToFind" parameter to "idSnippet" (or whatever), something like this should work:

Code:
if (tempElements[loop].id.indexOf(idSnippet) != -1) {

Of course, if you want a looser match (other than just substring match), you'd need to implement your own matching algorith in the "if" condition.

Hope this helps,
Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top