Skip to main content

HTML : Property vs Attribute

When writing HTML source code, you can define attributes on your HTML elements. Then, once the browser parses your code, a corresponding DOM node will be created. This node is an object, and therefore it has properties.
For instance, this HTML element:
<input type="text" value="Name:">
has 2 attributes.
Once the browser parses this code, a HTMLInputElement object will be created, and this object will contain dozens of properties like: accept, accessKey, align, alt, attributes, autofocus, baseURI, checked, childElementCount, childNodes, children, classList, className, clientHeight, etc.
For a given DOM node object, properties are the properties of that object, and attributes are the elements of the attributes property of that object.
When a DOM node is created for a given HTML element, many of its properties relate to attributes with the same or similar names, but it's not a one-to-one relationship. For instance, for this HTML element:
<input id="the-input" type="text" value="Name:">
the corresponding DOM node will have id,type, and value properties (among others):
  • The id property is a reflected property for the id attribute: Getting the property reads the attribute value, and setting the property writes the attribute value. id is a pure reflected property, it doesn't modify or limit the value.
  • The type property is a reflected property for the type attribute: Getting the property reads the attribute value, and setting the property writes the attribute value. type isn't a pure reflected property because it's limited to known values (e.g., the valid types of an input). If you had <input type="foo">, then theInput.getAttribute("type") gives you "foo" but theInput.type gives you "text".
  • In contrast, the value property doesn't reflect the value attribute. Instead, it's the current value of the input. When the user manually changes the value of the input box, the valueproperty will reflect this change. So if the user inputs "John" into the input box, then:
    theInput.value // returns "John"
    whereas:
    theInput.getAttribute('value') // returns "Name:"
    The value property reflects the current text-content inside the input box, whereas the valueattribute contains the initial text-content of the value attribute from the HTML source code.
    So if you want to know what's currently inside the text-box, read the property. If you, however, want to know what the initial value of the text-box was, read the attribute. Or you can use the defaultValue property, which is a pure reflection of the value attribute:
    theInput.value                 // returns "John"
    theInput.getAttribute('value') // returns "Name:"
    theInput.defaultValue          // returns "Name:"
  • Sources : https://stackoverflow.com/questions/6003819/what-is-the-difference-between-properties-and-attributes-in-html

Comments

Popular posts from this blog

Flash Tool Error Codes -MTK Chipset

Complete List of SP Flash Tool Error Codes and their Meanings and Solutions MTK Chipset. . How to Fix SP Flash Tool Errors (BROM Error Codes): SP Flash Tool Failed to enumerate COM Port This error clearly means that the Flash Tool has issues finding the COM port on which your device is connected. Solution: Connect your watch to other Port and make sure you have USB Drivers installed. Open “Device Manager” and find the COM Port of the device you connected. Once found, open Flash Tool, click on Options -> COM Port -> Select the COM Port on which you have connected your device. Device automatically disconnects during flashing Meaning: Device disconnects as soon as the flashing process begins, interrupting the process. Solution: Try a different USB Cord, USB Port & PC Hold the Volume down or up button while connecting the device to PC for flashing SP Flash Tool remains at 0% Solution: Install necessary MediaTek VCOM drivers on PC. Use the latest version of S...

Fiberglass DIY

Method Mix Fiberglass Substance Weigh a certain amount of resin, then add cobalt water according to 0.8% -1% of the resin amount, and stir evenly (  packing under 3 kg, the accelerator has been added, skip this step  ) Add the curing agent again at 2% -2.5% of the resin volume, and stir evenly. The fixing speed of this series of products is closely related to the ambient temperature and humidity. According to the temperature, adjust the proportion of curing agent and cobalt water appropriately. Reference data, add at room temperature 25 ℃, gel time about 9-15 minutes.  The curing time is 20-30 minutes, the above ratio is for reference only, the actual operation is subject to the site requirements. Proportion 100ml: (0.8-1)ml <----->100ml: (2-2.5)ml Resin: accelerator  <----->  Resin: curing agent Method of Repairing FRP: Grind the glass fiber reinforced plastic surface to be repaired with sandpaper first. Brush the tuned resin ...

Component vs Directives

Component and Directive is very important in Angular 2. Today we sharing difference between component and directive in Angular 2. Components Directive For register component we use @Component meta-data annotation. For register directives we use @Directive meta-data annotation. Component is a directive which use  shadow  DOM to create encapsulate visual behavior called components.  Components are typically used to create UI widgets. Directives is used to add behavior to an existing DOM element. Component is used to break up the application into smaller components. Directive is use to design re-usable components. Only one component can be present per DOM element. Many directive can be used in a per DOM element. @View decorator or templateurl template are mandatory in the component. Directive don’t have View. Component is used to define pipes. You can’t define Pipes in directive. viewEncapsulation can be define in compon...