HTML - attributes - ond...

revision:


Content

"ondblclick" attribute : mouse double-click on element drag attributes "ondurationchange" attribute : duration of audio/video changed


"ondblclick" attribute : mouse double-click on element

top

This attribute fires on a mouse double-click on the element.
Supported HTML tags: all HTML elements, EXCEPT: <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, and <title>

syntax

<element ondblclick="script"></element>

script: the script to be run on ondblclick.

some examples

example

A function is triggered when the button is double-clicked. The function outputs some text in a p element with id="demo".

codes:

                  <button style="margin-left:3vw;" ondblclick="clickTwice()">Double-click me</button>    
                  <p class="spec" id="demo" style="color:red"></p>
                  <p class="spec">A function is triggered when the button is double-clicked.
                  The function outputs some text in a p element with id="demo".</p>
                  <script>
                      function clickTwice() {
                        document.getElementById("demo").innerHTML = "Hello World";
                      }
                  </script>
              

example

Field1:

Field2:


A function is triggered when the button is double-clicked. The function copies the text from Field1 into Field2.

codes:

                  <p class="spec">Field1: <input type="text" id="field1" value="Hello World!"></p>
                  <p class="spec">Field2: <input type="text" id="field2"></p><br>
                  <button style="margin-left:3vw;" ondblclick="clickAgain()">Copy Text</button>
                  <p class="spec">A function is triggered when the button is double-clicked.
                  The function copies the text from Field1 into Field2.</p>
                  <script>
                      function clickAgain() {
                        document.getElementById("field2").value = document.getElementById("field1").value;
                      }
                  </script>
              

drag attributes

top

Links and images are draggable by default, and do not need the "draggable" attribute.
There are many event attributes that are used, and can occur, in the different stages of a drag and drop operation:

events fired on the draggable target (the source element):

ondragstart - fires when the user starts to drag an element;
ondrag - fires when an element is being dragged;
ondragend - fires when the user has finished dragging the element

events fired on the drop target:

ondragenter - fires when the dragged element enters the drop target;
ondragover - fires when the dragged element is over the drop target;
ondragleave - fires when the dragged element leaves the drop target;
ondrop - fires when the dragged element is dropped on the drop target.

Note: While dragging an element, the ondrag event fires every 350 milliseconds.

The drag-related attributes are part of the event attributes, and can be used on any HTML element.

"ondrag" attribute : the attribute fires when an element or text selection is being dragged

Note: While dragging an element, the ondrag event fires every 350 milliseconds.

The ondrag attribute is part of the event attributes, and can be used on any HTML element.

syntax: <element ondrag="script"></element >

script: the script to be run on ondrag.

"ondragend" attribute : fires when the user has finished dragging an element or text selection.

syntax: <element ondragend="script"></element >

script: the script to be run on ondragend.

"ondragenter" attribute: fires when a draggable element or text selection enters a valid drop target

syntax: <element ondragenter ="script"></element >

script: the script to be run on ondragenter.

"ondragleave" attribute : fires when a draggable element or text selection leaves a valid drop target.

syntax: <element ondragleave ="script"></element>

script: the script to be run on ondragleave.

"ondragover" attribute : fires when a draggable element or text selection is being dragged over a valid drop target

By default, data/elements cannot be dropped in other elements. To allow a drop, we must prevent the default handling of the element. This is done by calling theevent.preventDefault() method for the ondragover attribute.

syntax: <element ondragover="script"></element >

script: the script to be run on ondragover.

"ondragstart" attribute : fires when the user starts to drag an element or text selection.

By default, data/elements cannot be dropped in other elements. To allow a drop, we must prevent the default handling of the element. This is done by calling theevent.preventDefault() method for the ondragover attribute.

syntax: <element ondragstart="script"></element>

script: the script to be run on ondragstart.

"ondrop" attribute : fires when a draggable element or text selection is dropped on a valid drop target.

syntax: <element ondrop="script"></element>

script: the script to be run on ondrop.

some examples

example

Drag the p element back and forth between the two rectangles:

Drag me!

code:
                  <div>
                    <p class="spec">Drag the p element back and forth between the two rectangles:</p>
                    <div class="spec droptarget-1a" ondrop="drop(event)" ondragover="allowDrop(event)">
                        <p class="spec" ondragstart="dragStart(event)" ondrag="dragging(event)" draggable="true" id="dragtarget-1a">Drag me!</p>
                    </div>
                    <div style="margin-left:3vw;" class="droptarget-1a" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
                    <p class="spec" style="clear:both;" id="demo-1a"></p>
                </div>
                <script>
                    function dragStart(event) {
                      event.dataTransfer.setData("Text-1a", event.target.id);
                    }
                    function dragging(event) {
                      document.getElementById("demo-1a").innerHTML = "The p element is being dragged";
                    }
                    function allowDrop(event) {
                      event.preventDefault();
                    }
                    function drop(event) {
                      event.preventDefault();
                      var data = event.dataTransfer.getData("Text-1a");
                      event.target.appendChild(document.getElementById(data));
                      document.getElementById("demo-1a").innerHTML = "The p element was dropped";
                    }
                </script>
                <style>
                    .droptarget-1a {float: left; width: 10vw; height: 3.5vw; margin: 1.5vw; padding: 1vw; border: 4px solid darkgreen;}
                </style>
              

example

Drag the p element back and forth between the two rectangles:

Drag me!

code:
                    <div>
                      <p class="spec">Drag the p element back and forth between the two rectangles:</p>
                      <div class="droptarget-2a" ondrop="drop(event)" ondragover="allowDrop(event)">
                          <p class="spec" ondragstart="dragStart(event)" ondragend="dragEnd(event)" draggable="true" id="dragtarget-2a">Drag me!</p>
                      </div>
                      <div class="droptarget-2a" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
                      <p class="spec" style="clear:both;" id="demo-2a"></p>
                  </div>
                  <script>
                      function dragStart(event) {
                        event.dataTransfer.setData("Text-2a", event.target.id);
                        document.getElementById("demo-2a").innerHTML = "Started to drag the p element";
                      }
          
                      function dragEnd(event) {
                        document.getElementById("demo-2a").innerHTML = "Finished dragging the p element.";
                      }
          
                      function allowDrop(event) {
                        event.preventDefault();
                      }
          
                      function drop(event) {
                        event.preventDefault();
                        var data = event.dataTransfer.getData("Text-2a");
                        event.target.appendChild(document.getElementById(data));
                      }
                  </script>
                  <style>
                      .droptarget-2a {float: left; width: 10vw; height: 3.5vw; margin: 1.5vw; padding: 1vw; border: 4px solid red;}
                  </style>
              

example

Drag the p element back and forth between the two rectangles:

Drag me!

code:
                    <div>
                      <p class="spec">Drag the p element back and forth between the two rectangles:</p>
                      <div class="droptarget-3a" ondrop="drop(event)" ondragenter="dragEnter(event)" ondragleave="dragLeave(event)" 
                      ondragover="allowDrop(event)">
                        <p class="spec" ondragstart="dragStart(event)" draggable="true" id="dragtarget-3a">Drag me!</p>
                      </div>
                      <div class="droptarget-3a" ondragenter="dragEnter(event)" ondragleave="dragLeave(event)" ondrop="drop(event)" 
                      ondragover="allowDrop(event)"></div>
                      <p class="spec" style="clear:both;" id="demo-3a"></p>
                  </div>
                  <script>
                      function dragStart(event) {
                        event.dataTransfer.setData("Text-3a", event.target.id);
                      }
          
                      function dragEnter(event) {
                        if ( event.target.className == "droptarget-3a" ) {
                          event.target.style.border = "3px dotted red";
                          document.getElementById("demo-3a").innerHTML = "Entered the dropzone";
                        }
                      }
          
                      function dragLeave(event) {
                        if ( event.target.className == "droptarget-3a" ) {
                          event.target.style.border = "";
                          document.getElementById("demo-3a").innerHTML = "Left the dropzone";
                        }
                      }
          
                      function allowDrop(event) {
                        event.preventDefault();
                      }
          
                      function drop(event) {
                        event.preventDefault();
                        var data = event.dataTransfer.getData("Text-3a");
                        event.target.appendChild(document.getElementById(data));
                      }
                  </script>
                  <style>
                      .droptarget-3a {float: left; width: 10vw; height: 3.5vw; margin: 1.5vw; padding: 1vw; border: 4px solid darkgreen;}
                  </style>
              
              

example

Drag the p element back and forth between the two rectangles:

Drag me!

code:
                    <div>
                      <p class="spec">Drag the p element back and forth between the two rectangles:</p>
                      <div class="droptarget-4a" ondrop="drop(event)" ondragenter="dragEnter(event)" ondragleave="dragLeave(event)" 
                      ondragover="allowDrop(event)">
                          <p class="spec" ondragstart="dragStart(event)" draggable="true" id="dragtarget-4a">Drag me!</p>
                      </div>
                      <div class="droptarget-4a" ondragenter="dragEnter(event)" ondragleave="dragLeave(event)" ondrop="drop(event)" 
                      ondragover="allowDrop(event)"></div>
                      <p class="spec" style="clear:both;" id="demo-4a"></p>
                  </div>
                  <script>
                      function dragStart(event) {
                        event.dataTransfer.setData("Text-4a", event.target.id);
                      }
                      
                      function dragEnter(event) {
                        if ( event.target.className == "droptarget-4a" ) {
                          event.target.style.border = "3px dotted red";
                          document.getElementById("demo-4a").innerHTML = "Entered the dropzone";
                        }
                      }
                      
                      function dragLeave(event) {
                        if ( event.target.className == "droptarget-4a" ) {
                          event.target.style.border = "";
                          document.getElementById("demo-4a").innerHTML = "Left the dropzone";
                        }
                      }
                      
                      function allowDrop(event) {
                        event.preventDefault();
                      }
                      
                      function drop(event) {
                        event.preventDefault();
                        var data = event.dataTransfer.getData("Text-4a");
                        event.target.appendChild(document.getElementById(data));
                      }
                  </script>
                  <style>
                      .droptarget-4a { float: left; width: 10vw; height: 3.5vw; margin: 1.5vw; margin-right: 10vw; padding: 1vw; border: 4px solid pink;}
                  </style>
              

example

Drag me into the rectangle!

code:
                    <div>
                      <p class="spec" ondragstart="dragStart(event)" draggable="true" id="dragtarget-5a">Drag me into the rectangle!</p>
                      <div id="droptarget-5a" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
                      <p class="spec" style="clear:both;" id="demo-5a"></p>
                  </div>
                  <script>
                      function dragStart(event) {
                          event.dataTransfer.setData("Text-5a", event.target.id);
                      }
          
                      function allowDrop(event) {
                          event.preventDefault();
                          document.getElementById("demo-5a").innerHTML = "The p element is OVER the droptarget.";
                          event.target.style.border = "4px dotted green";
                      }
          
                      function drop(event) {
                          event.preventDefault();
                          var data = event.dataTransfer.getData("Text-5a");
                          event.target.appendChild(document.getElementById(data));
                          document.getElementById("demo-5a").innerHTML = "The p element was dropped.";
                      }
                  </script>
                  <style>
                      #droptarget-5a { float: left; width: 15vw; height: 5vw; margin: 1.5vw; margin-right: 10vw; padding: 1vw; border: 4px solid burlywood;}
                  </style>
              

example

Drag the p element back and forth between the two rectangles:

Drag me!

code:
                    <div>
                      <p class="spec">Drag the p element back and forth between the two rectangles:</p>
                      <div class="droptarget-6a" ondrop="drop(event)" ondragover="allowDrop(event)">
                        <p class="spec" ondragstart="dragStart(event)" ondragend="dragEnd(event)" draggable="true" id="dragtarget-6a">Drag me!</p>
                      </div>
                      <div class="droptarget-6a" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
                      <p class="spec" style="clear:both;" id="demo-6a"></p>
                  </div>
                  <script>
                      function dragStart(event) {
                          event.dataTransfer.setData("Text-6a", event.target.id);
                          document.getElementById("demo-6a").innerHTML = "Started to drag the p element";
                      }
          
                      function dragEnd(event) {
                          document.getElementById("demo-6a").innerHTML = "Finished dragging the p element.";
                      }
          
                      function allowDrop(event) {
                          event.preventDefault();
                      }
          
                      function drop(event) {
                          event.preventDefault();
                          var data = event.dataTransfer.getData("Text-6a");
                          event.target.appendChild(document.getElementById(data));
                      }
                  </script>
                  <style>
                      .droptarget-6a {float: left; width: 10vw; height: 3.5vw; margin: 1.5vw; padding: 1vw; border: 4px solid lightgreen;}
                  </style>
              

example

Drag the p element back and forth between the two rectangles:

Drag me!

code:
                    <div>
                      <p class="spec">Drag the p element back and forth between the two rectangles:</p>
                      <div class="droptarget-7a" ondrop="drop(event)" ondragover="allowDrop(event)">
                          <p class="spec" ondragstart="dragStart(event)" draggable="true" id="dragtarget-7a">Drag me!</p>
                      </div>
                      <div class="droptarget" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
                      <p class="spec" style="clear:both;" id="demo-7a"></p>
                  </div>
                  <style>
                      .droptarget-7a {float: left; width: 10vw; height: 3.5vw; margin: 1.5vw; padding: 1vw; border: 0.4vw solid magenta;}
                  </style>
                  <script>
                      function dragStart(event) {
                          event.dataTransfer.setData("Text-7a", event.target.id);
                          document.getElementById("demo-7a").innerHTML = "Started to drag the p element";
                      }
                      function allowDrop(event) {
                          event.preventDefault();
                      }
                      function drop(event) {
                          event.preventDefault();
                          var data = event.dataTransfer.getData("Text-7a");
                          event.target.appendChild(document.getElementById(data));
                          document.getElementById("demo-7a").innerHTML = "The p element was dropped";
                      }
                  </script>
              

"ondurationchange" attribute : duration of audio/video changed

top

The ondurationchange event occurs when the duration data of the specified audio/video is changed. The ondurationchange attribute is part of the event attributes and can be used on the following elements: <audio>, <video>.

syntax

<video ondurationchange="script"></video >

some examples

example

example audio:

                  <video style="margin-left:3vw;" controls ondurationchange="videoChange()">
                      <source src="Wuzhen-20-10_02.mp4" type="video/mp4">
                      Your browser does not support HTML5 video.
                </video>
                <script>
                      function videoChange() {
                      alert("The video duration has changed");
                      }
                  </script>
              

example video :

                <audio id="myAudio" controls ondurationchange="audioChange(this)">
                    <source style="margin-left:3vw;" src="horse.wav" type="audio/wav">
                    Your browser does not support the audio element.
                </audio>
                <script>
                    function audioChange(x) {
                      alert("The duration of this audioclip is : " + x.duration + " seconds");
                    }
                </script>