Get All Elements with id starting with "prefix"

Sciter Forums Native Get All Elements with id starting with "prefix"

Tagged: 

This topic contains 4 replies, has 2 voices, and was last updated by  Human Being 1 month, 1 week ago.

  • Author
    Posts
  • #49345

    Human Being
    Participant

    Hi, I’m trying to write code to extract all nodes that have an id beginning with a certain prefix.
    Some thing like the following:

    <div id="exec_0"></div>
    <div id="exec_1"></div>
    <div id="exec_2"></div>

    Then in C++

    std::vector<sciter::dom::element> nodes = getAllNodes("div[id^=exec_]");

    I tried find_all(&counter, “selector”); but im not sure how it works.

    Thanks

    • This topic was modified 1 month, 1 week ago by  Human Being.
    • This topic was modified 1 month, 1 week ago by  Human Being.
    • This topic was modified 1 month, 1 week ago by  Human Being.
  • #49352

    Andrew
    Keymaster
    std::vector<sciter::dom::element> find_all_elements(sciter::dom::element root, const char* selector) {
      struct each_callback : public sciter::dom::callback {
         std::vector<sciter::dom::element> elements;
         virtual bool on_element(HELEMENT he) {
           elements.push_back( sciter::dom::element(he) );
           return false; // keep going
         }
      };
      each_callback cb;
      root.find_all(&cb,selector);
      return each_callback.elements; 
    }
    • #49365

      Human Being
      Participant

      Thanks. It works for divs with classes but not for the following selectors:

      div[id^='prefix_']
      div[id~='prefix_']
      div[id|='prefix_']
  • #49366

    Andrew
    Keymaster

    Try this in sciter.exe:

    <html>
        <head>
            <title>Test</title>
            <style></style>
            <script type="text/tiscript">
    
               function self.ready() {
                  var list = self.$$(div[id^=exec_]);
                  $(#total).value = list.length;
               }
    
            </script>
        </head>
        <body>
          <div id="exec_0"></div>
          <div id="exec_1"></div>
          <div id="exec_2"></div>
          <div id="query_1"></div>
          <div id="query_2"></div>
          Number of exec divs is <output#total />
        </body>
    </html>

    That self.$$(div[id^=exec_]) uses exactly the same functionality as does find_all_elements(root,"div[id^=exec_]") above.

    • #49395

      Human Being
      Participant

      Thanks. It works, my mistake.

You must be logged in to reply to this topic.