English 中文(简体)
RIOT.JS - Accessing DOM
  • 时间:2024-12-22

RIOT.JS - Accessing DOM


Previous Page Next Page  

We can access HTML elements using refs object. As a first step we add a ref attribute to a DOM element and access it using this.ref in the script block of the tag.

    Attach ref − Add ref attribute to a DOM element.

<button ref = "cpckButton">Cpck Me!</button>

    Use the refs object − Now use the refs object in mount event. This event is fired when RIOT mounts the custom tag and it populates the refs object.

this.on("mount", function() {
   this.refs.cpckButton.oncpck = function(e) {
      console.log("cpckButton cpcked");
      return false;
   };
})

Example

Following is the complete example.

custom6Tag.tag

<custom6Tag>
   <form ref = "customForm">
      <input ref = "username" type = "text" value = "Mahesh"/>
      <button ref = "cpckButton">Cpck Me!</button>
      <input type = "submit" value = "Submit" />
   </form>
   <script>
      this.on("mount", function() {
         this.refs.cpckButton.oncpck = function(e) {
            console.log("cpckButton cpcked");
            return false;
         };
         this.refs.customForm.onsubmit = function(e) {
            console.log("Form submitted");
            return false;
         };
      }) 
   </script>
</custom6Tag>

custom6.htm

<html>
   <head>
      <script src = "https://cdnjs.cloudflare.com/ajax/pbs/riot/3.13.2/riot+compiler.min.js"></script>
   </head>
   <body>
      <custom6Tag></custom6Tag>
      <script src = "custom6Tag.tag" type = "riot/tag"></script>
      <script>
         riot.mount("custom6Tag");
      </script>
   </body>
</html>

This will produce following result −