How can I select all elements with the same class name?

后端 未结 4 2036
我在风中等你
我在风中等你 2021-01-07 10:30

I have a Boolean variable. It is stored in a hidden input field. Basically, if the user is signed in, it is false, if not, it is true.

Ther

相关标签:
4条回答
  • 2021-01-07 11:07
    if (document.getElementById('download_btn_var_input').value == "true") {
        var el = document.getElementsByClassName('project_download_btn');
        for (var i = 0; i < el.length; i++) {
            el[i].hidden = true;
        }
    }
    
    0 讨论(0)
  • 2021-01-07 11:09

    document.getElementsByClassName returns array so what you are interested is :

    document.getElementsByClassName('project_download_btn')[0]
    
    0 讨论(0)
  • 2021-01-07 11:12

    Loop through each div that contains your download button and set hidden to true:

    if (document.getElementById('download_btn_var_input').value == "true") {
        var button_divs_array = document.getElementsByClassName('project_download_btn');
        for (var i = 0; i < button_divs_array.length; i++) {
            button_divs_array[i].hidden = true;
        }
    }
    
    0 讨论(0)
  • 2021-01-07 11:17

    You can simply loop through the elements instead of just taking the 0th.

    var buttons = document.getElementsByClassName('project_download_btn');
    
    for(var i=0; i< buttons.length; i++){
        buttons[i].hidden = true;
    }
    
    0 讨论(0)
提交回复
热议问题