Cara menggunakan remove target attribute javascript

Sets the value of an attribute on the specified element. If the attribute already exists, the value is updated; otherwise a new attribute is added with the specified name and value.

Show

    To get the current value of an attribute, use getAttribute(); to remove an attribute, call removeAttribute().

    setAttribute(name, value)
    

    name

    A string specifying the name of the attribute whose value is to be set. The attribute name is automatically converted to all lower-case when setAttribute() is called on an HTML element in an HTML document.

    value

    A string containing the value to assign to the attribute. Any non-string value specified is converted automatically into a string.

    Boolean attributes are considered to be true if they're present on the element at all. You should set value to the empty string (

    <button>Hello World</button>
    
    1) or the attribute's name, with no leading or trailing whitespace. See the below for a practical demonstration.

    Since the specified value gets converted into a string, specifying

    <button>Hello World</button>
    
    3 doesn't necessarily do what you expect. Instead of removing the attribute or setting its value to be
    <button>Hello World</button>
    
    3, it instead sets the attribute's value to the string
    <button>Hello World</button>
    
    5. If you wish to remove an attribute, call removeAttribute().

    None (

    <button>Hello World</button>
    
    7).

    <button>Hello World</button>
    
    8
    <button>Hello World</button>
    
    9

    The specified attribute name contains one or more characters which are not valid in attribute names.

    In the following example, setAttribute() is used to set attributes on a

    button {
      height: 30px;
      width: 100px;
      margin: 1em;
    }
    
    2.

    <button>Hello World</button>
    

    button {
      height: 30px;
      width: 100px;
      margin: 1em;
    }
    

    const button = document.querySelector("button");
    
    button.setAttribute("name", "helloButton");
    button.setAttribute("disabled", "");
    

    This demonstrates two things:

    • The first call to setAttribute() above shows changing the name attribute's value to "helloButton". You can see this using your browser's page inspector (Chrome, Edge, Firefox, Safari).
    • To set the value of a Boolean attribute, such as
      button {
        height: 30px;
        width: 100px;
        margin: 1em;
      }
      
      5, you can specify any value. An empty string or the name of the attribute are recommended values. All that matters is that if the attribute is present at all, regardless of its actual value, its value is considered to be true. The absence of the attribute means its value is
      button {
        height: 30px;
        width: 100px;
        margin: 1em;
      }
      
      7. By setting the value of the
      button {
        height: 30px;
        width: 100px;
        margin: 1em;
      }
      
      5 attribute to the empty string (
      <button>Hello World</button>
      
      1), we are setting
      button {
        height: 30px;
        width: 100px;
        margin: 1em;
      }
      
      5 to true, which results in the button being disabled.

    DOM methods dealing with element's attributes:

    Not namespace-aware, most commonly used methodsNamespace-aware variants (DOM Level 2)DOM Level 1 methods for dealing with
    const button = document.querySelector("button");
    
    button.setAttribute("name", "helloButton");
    button.setAttribute("disabled", "");
    
    2 nodes directly (seldom used)DOM Level 2 namespace-aware methods for dealing with
    const button = document.querySelector("button");
    
    button.setAttribute("name", "helloButton");
    button.setAttribute("disabled", "");
    
    2 nodes directly (seldom used)
    const button = document.querySelector("button");
    
    button.setAttribute("name", "helloButton");
    button.setAttribute("disabled", "");
    
    4 (DOM 1)
    const button = document.querySelector("button");
    
    button.setAttribute("name", "helloButton");
    button.setAttribute("disabled", "");
    
    5
    const button = document.querySelector("button");
    
    button.setAttribute("name", "helloButton");
    button.setAttribute("disabled", "");
    
    6
    const button = document.querySelector("button");
    
    button.setAttribute("name", "helloButton");
    button.setAttribute("disabled", "");
    
    7
    const button = document.querySelector("button");
    
    button.setAttribute("name", "helloButton");
    button.setAttribute("disabled", "");
    
    8 (DOM 1)
    const button = document.querySelector("button");
    
    button.setAttribute("name", "helloButton");
    button.setAttribute("disabled", "");
    
    9getAttribute()0getAttribute()1getAttribute()2 (DOM 2)getAttribute()3--getAttribute()4 (DOM 1)getAttribute()5getAttribute()6-

    Specification

    BCD tables only load in the browserwith JavaScript enabled. Enable JavaScript to view data.

    Using setAttribute() to modify certain attributes, most notably value in XUL, works inconsistently, as the attribute specifies the default value. To access or modify the current values, you should use the properties. For example, use getAttribute()9 instead of removeAttribute()0.