How can I select an element by name with jQuery?

前端 未结 14 2812
迷失自我
迷失自我 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 06:05

    You could get the array of elements by name the old fashioned way and pass that array to jQuery.

    function toggleByName() {
      var arrChkBox = document.getElementsByName("chName");
      $(arrChkBox).toggle();
    }
    
    
      
        sandBox
      
      
        



    note: the only time you would have a reason to use the "name" attribute should be for checkbox or radio inputs.

    Or you could just add another class to the elements for selection.(This is what I would do)

    function toggleByClass(bolShow) {
      if (bolShow) {
        $(".rowToToggle").show();
      } else {
        $(".rowToToggle").hide();
      }
    }
    
    
      
        sandBox
      
      
        
    data1 data2
    data1 data2
    data1 data2

提交回复
热议问题