How can I select an element by name with jQuery?

前端 未结 14 2526
迷失自我
迷失自我 2020-11-22 05:13

Have a table column I\'m trying to expand and hide:

jQuery seems to hide the td elements when I select it by class but not by element\'s name.

<
相关标签:
14条回答
  • 2020-11-22 05:50

    You can use the function:

    get.elementbyId();
    
    0 讨论(0)
  • 2020-11-22 05:53

    Any attribute can be selected using [attribute_name=value] way. See the sample here:

    var value = $("[name='nameofobject']");
    
    0 讨论(0)
  • 2020-11-22 05:55

    If you have something like:

    <input type="checkbox" name="mycheckbox" value="11" checked="">
    <input type="checkbox" name="mycheckbox" value="12">
    

    You can read all like this:

    jQuery("input[name='mycheckbox']").each(function() {
        console.log( this.value + ":" + this.checked );
    });
    

    The snippet:

    jQuery("input[name='mycheckbox']").each(function() {
      console.log( this.value + ":" + this.checked );
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input type="checkbox" name="mycheckbox" value="11" checked="">
    <input type="checkbox" name="mycheckbox" value="12">

    0 讨论(0)
  • 2020-11-22 05:56

    Performance

    Today (2020.06.16) I perform tests for chosen solutions on MacOs High Sierra on Chrome 83.0, Safari 13.1.1 and Firefox 77.0.

    Conclusions

    Get elements by name

    • getElementByName (C) is fastest solution for all browsers for big and small arrays - however I is probably some kind of lazy-loading solution or It use some internal browser hash-cache with name-element pairs
    • mixed js-jquery solution (B) is faster than querySelectorAll (D) solution
    • pure jquery solution (A) is slowest

    Get rows by name and hide them (we exclude precalculated native solution (I) - theoretically fastest) from comparison - it is used as reference)

    • surprisingly the mixed js-jquery solution (F) is fastest on all browsers
    • surprisingly the precalculated solution (I) is slower than jquery (E,F) solutions for big tables (!!!) - I check that .hide() jQuery method set style "default:none" on hidden elements - but it looks that they find faster way of do it than element.style.display='none'
    • jquery (E) solution is quite-fast on big tables
    • jquery (E) and querySelectorAll (H) solutions are slowest for small tables
    • getElementByName (G) and querySelectorAll (H) solutions are quite slow for big tables

    Details

    I perform two tests for read elements by name (A,B,C,D) and hide that elements (E,F,G,H,I)

    • small table - 3 rows - you can run test HERE
    • big table - 1000 rows - you can run test HERE

    Snippet below presents used codes

    //https://stackoverflow.com/questions/1107220/how-can-i-select-an-element-by-name-with-jquery#
    
    // https://jsbench.me/o6kbhyyvib/1
    // https://jsbench.me/2fkbi9rirv/1
    
    function A() {
      return $('[name=tcol1]');
    }
    
    function B() {
      return $(document.getElementsByName("tcol1"))
    }
    
    function C() {
      return document.getElementsByName("tcol1")
    }
    
    function D() {
      return document.querySelectorAll('[name=tcol1]')
    }
    
    function E() {
      $('[name=tcol1]').hide();
    }
    
    function F() {
      $(document.getElementsByName("tcol1")).hide();
    }
    
    function G() {
      document.getElementsByName("tcol1").forEach(e=>e.style.display='none'); 
    }
    
    function H() {
      document.querySelectorAll('[name=tcol1]').forEach(e=>e.style.display='none'); 
    }
    
    function I() {
      let elArr = [...document.getElementsByName("tcol1")];
      let length = elArr.length
      for(let i=0; i<length; i++) elArr[i].style.display='none';
    }
    
    
    
    
    // -----------
    // TEST
    // -----------
    
    function reset() { $('td[name=tcol1]').show(); } 
    
    [A,B,C,D].forEach(f=> console.log(`${f.name} rows: ${f().length}`)) ;
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <div>This snippet only presents used codes</div>
    <table>
      <tr>    
          <td>data1</td>
          <td name="tcol1" class="bold"> data2</td>
      </tr>
      <tr>    
          <td>data1</td>
          <td name="tcol1" class="bold"> data2</td>
      </tr>  
      <tr>    
          <td>data1</td>
          <td name="tcol1" class="bold"> data2</td>
      </tr>
    </table>
    
    <button onclick="E()">E: hide</button>
    <button onclick="F()">F: hide</button>
    <button onclick="G()">G: hide</button>
    <button onclick="H()">H: hide</button>
    <button onclick="I()">I: hide</button><br>
    <button onclick="reset()">reset</button>

    Example results on Chrome

    0 讨论(0)
  • 2020-11-22 05:57

    You forgot the second set of quotes, which makes the accepted answer incorrect:

    $('td[name="tcol1"]') 
    
    0 讨论(0)
  • 2020-11-22 06:00

    You can get the element in JQuery by using its ID attribute like this:

    $("#tcol1").hide();
    
    0 讨论(0)
提交回复
热议问题