wildcard in js?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    wildcard in js?

    There are few DIVs in my HTML who have ids in the form of randomString+ '_N'

    eg
    [html]<div id="fhgse_N" class="hq"></div>
    <div id="dgf5g_N" class="hq"></div>
    <div id="ns65h_N" class="hq"></div>[/html]

    I need to provide innerHTML to them by splitting the response text string from AJAX.

    Since I don't know what the random strings (prefixed to the IDs) would be, but I know that the suffix is _N, the class name is hq and tag name is div.

    How can I set innerHTML of these DIVs?
    Wildcard could help in that.
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    [CODE=javascript]function get_nodes_by_cl ass(obj, class_name) {
    var node_list = [];
    var nodes = document.getEle mentsByTagName( obj);

    for (var i = 0, n; n = nodes[i]; i++) {
    if (n.className == class_name) {
    node_list.push( n);
    }
    }

    return node_list;
    }

    // usage
    var hq_divs = get_nodes_by_cl ass('div', 'hq');
    [/CODE]
    now simply iterate over the 'hq_divs' and set innerHTML :)

    kind regards

    Comment

    • hsriat
      Recognized Expert Top Contributor
      • Jan 2008
      • 1653

      #3
      Thanks, I just made a little change and its working!!
      :)


      [CODE=javascript]function get_nodes_by_cl ass(obj, class_name, id_suffix) {
      var node_list = [];
      var nodes = document.getEle mentsByTagName( obj);

      for (var i = 0, n; n = nodes[i]; i++) {
      if (n.className == class_name && n.id.match(id_s uffix)) {
      node_list.push( n);
      }
      }

      return node_list;
      }

      // usage
      var hq_divs = get_nodes_by_cl ass('div', 'hq', '_N');[/code]

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5388

        #4
        ok :) to make it 'sure' working:

        [CODE=javascript]function get_nodes_by_cl ass(obj, class_name, id_suffix) {
        var node_list = [];
        var nodes = document.getEle mentsByTagName( obj);
        var re = new RegExp(id_suffi x + '$');

        for (var i = 0, n; n = nodes[i]; i++) {
        if (n.className == class_name && n.id.match(re)) {
        node_list.push( n);
        }
        }

        return node_list;
        }

        // usage
        var hq_divs = get_nodes_by_cl ass('div', 'hq', '_N');[/code]
        kind regards

        Comment

        • hsriat
          Recognized Expert Top Contributor
          • Jan 2008
          • 1653

          #5
          Why is id_suffix + '$' used?

          Comment

          • hsriat
            Recognized Expert Top Contributor
            • Jan 2008
            • 1653

            #6
            Ok, thanks:)

            I got it...

            Comment

            • gits
              Recognized Expert Moderator Expert
              • May 2007
              • 5388

              #7
              Originally posted by hsriat
              Ok, thanks:)

              I got it...
              it is used for the regEx to check from the end of the string ... ;)

              kind regards

              Comment

              Working...