SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Guru GamerZ's Avatar
    Join Date
    Nov 2001
    Location
    Singapore
    Posts
    617
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    getElementsByName Wildcard

    Good evening guys,

    I would like to ask is there any way I can use document.getElementsByName("polls-*") and it will get all elelments which starts with the name "polls-" ?

    Thanks in advanced.

  2. #2
    SitePoint Addict
    Join Date
    Apr 2001
    Location
    Devon, UK
    Posts
    333
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    When you say start with the name "polls-", do you mean the ID? e.g. <div id="polls-1">?

    If so, then yes, it's possible - but you'd need a JavaScript function to do it. That function would have to recurse the DOM, check the ID against a regular expression, and return a collection (or array) of valid nodes.

    We can certainly help you with that - please get in touch if you are interested.

  3. #3
    SitePoint Guru GamerZ's Avatar
    Join Date
    Nov 2001
    Location
    Singapore
    Posts
    617
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi there,

    Yea that is what I meant, I also thought of using the recuse way, I thought there might be an easier way out.

    Thanks =)

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would like to ask is there any way I can use document.getElementsByName("polls-*")
    No.

  5. #5
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Presumably there are seperations of possibliity to this question?

    In theory (and probably practice) it would be possible to emulate such a behaviour, but it would involve checking EVERY SINGLE ELEMENT firstly for the presence of the attribute, then running a dynamically compiled reg exp.

    GamerZ, in reality i would follow with 7Stud, practically this is not possible, or more so, this should not be possible.

    Could we ask what functioanality it is you are trying to achieve, and maybe someone can suggest an alternative methodology?

    Many thanks

    /Matt

  6. #6
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I suppose you could limit the recursion by using getElementsByTagName, and then parsing that collection for the name="" part? (Assumes that all elements you seek have the same tag type e.g. <div> )
    Ian Anderson
    www.siteguru.co.uk

  7. #7
    &#083;itePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It has been noted before, but perhaps it should be again: the name attribute is supported only by a limited set of elements. The div element is not among those.

  8. #8
    SitePoint Addict
    Join Date
    Apr 2001
    Location
    Devon, UK
    Posts
    333
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Recursing every page element is only a problem if you have a really, really long page. Even then, the work would be done quickly. Most JS libraries are provided with a getElementByClass type of function which does it.

    In this case, GamerZ, you can limit the recursion by:

    1. Starting at a node deeper in the DOM, e.g. if you know that all the elements are contained within, say, <div id="allposts">.

    2. Only examining DIVs for the correct name, rather than any node.

    3. Ignore processing of any text, attributes, comments, or other non-element nodes.

  9. #9
    SitePoint Zealot alexisb's Avatar
    Join Date
    Aug 2003
    Location
    Lima, Peru
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Most Javascript libraries allow you to traverse your documents using many easy to understand selectors, for instance, take a look at jQuery selectors, which supports the XPath-like syntax E[@foo^=bar].

    Of course you could write your own Javascript functions but most times a solid library will be more than enough.

  10. #10
    SitePoint Addict
    Join Date
    Apr 2001
    Location
    Devon, UK
    Posts
    333
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Getting a library such as Prototype or Yahoo! UI is an option, but they can be huge and will add a lot of weight to your pages. Only use them if you intend to make use of several other facilities.

    There's no real substitute to writing your own JS libraries. You'll find it easier to debug your own script, you'll learn a lot, and then there's the enormous sense of satisfaction!

    In this particular case, the function could be written in a couple of dozen lines.

  11. #11
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ceeb View Post
    Getting a library such as Prototype or Yahoo! UI is an option, but they can be huge and will add a lot of weight to your pages. Only use them if you intend to make use of several other facilities.
    I believe both Prototype and Yahoo! UI are both pretty big, but jQuery is under 20KB (trimmed)! It also manages to pack a lot of functionality into it.

  12. #12
    SitePoint Zealot alexisb's Avatar
    Join Date
    Aug 2003
    Location
    Lima, Peru
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Agree, jQuery is pretty small and focus on very simple tasks, not the great effects we see everywhere these days but gets the job done, it can do what was asked in this thread


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •