Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add javascript function to html tag?

Just curious, and want to know. Can I add a javascript function to certain html tag? In example, if I have div tag like this:

<div id="example">Hello!</div>

and want to add function that alert the value inside of every div like:

function divFunction(){
     alert(/*some div content*/);
}

so, it can be executed like:

<div id="example">Hello!</div>
<script>
     document.getElementById("example").divFunction();
</script>

The point of my question is to add some new attribute or function to a certain tags. Like if there is:

document.getElementById("example").innerHTML;

can it be:

document.getElementById("example").newAttribute;

What makes me curious because jQuery can done something like this:

$( "#result" ).load( "ajax/test.html" );
like image 766
user2265229 Avatar asked Sep 07 '17 08:09

user2265229


People also ask

How do you reference a JavaScript function in HTML?

To include our JavaScript file in the HTML document, we have to use the script tag <script type = "text/javascript" src = "function. js"> and in the "src" attribute we have to provide the path to our JavaScript file where it is stored.

How do I connect JavaScript and HTML?

We can link JavaScript to HTML by adding all the JavaScript code inside the HTML file. We achieve this using the script tag which was explained earlier. We can put the <script></script> tag either inside the head of the HTML or at the end of the body.

How do you call a function inside a div tag?

You can name each function according to the div 's id and call it dynamically using the object["methodName"]() syntax to call it.

How to insert JavaScript in HTML with script tag?

There are two ways you can use the script tag to insert JavaScript in HTML. You can insert JavaScript directly into an HTML file. Here is an example of how you would do that using the script tag. I will go ahead and start with the script tag. Between the script tag, I will create a function that prints the text “Hello World” to the console.

What is the correct method for adding JavaScript to HTML?

The script tag is the correct method for adding Javascript to HTML. HTML cannot access or change Javascript code inside a script tag, but Javascript can access or change HTML. Variables created within one script tag cannot be accessed by another script tag somewhere else on a web page.

How to add JavaScript code directly in the HTML event?

Generally, this method is used when we have to call a function in the HTML event attributes. There are many cases (or events) in which we have to add JavaScript code directly eg., OnMover event, OnClick, etc. Let's see with the help of an example, how we can add JavaScript directly in the html without using the <script>.... </script> tag.

What are the different types of HTML JavaScript tags?

HTML JavaScript 1 The HTML <script> Tag. The HTML <script> tag is used to define a client-side script (JavaScript). ... 2 A Taste of JavaScript 3 The HTML <noscript> Tag 4 HTML Exercises 5 HTML Script Tags. For a complete list of all available HTML tags, visit our HTML Tag Reference.


2 Answers

DOM elements are like javascript objects with certain properties, so you could add a new property to, in your case a new function.

Here's an example:

var element = document.querySelector('#example');

function bindToNode(node, name, fn) {
  node[name] = fn.bind(node);
}

bindToNode(element, 'logValue', function() {
  console.log(this.textContent);
});

element.logValue();
<div id="example">
  Hello!
</div>

But you shouldn't mess arround with DOM elements, a better way would be to wrap the elements like jQuery does.

like image 146
cyr_x Avatar answered Oct 03 '22 04:10

cyr_x


This isn't possible because $ (jQuery) and document are defined objects that have there in built functions.

Because when you use . you call an object member.

Foo.bar() - Calling function bar() defined inside and object Foo.

So you would have to make your class/selector.

Here is a simple example:

class select {
	constructor(selector) {
  	this._element = document.getElementById(selector);
  }
  
  // Functions
  
  colorMe() {
  	this._element.style.backgroundColor = "red";
  }

  moveMe(x) {
    this._element.style.marginTop = x;
  }
}

var elem = new select("example");

elem.colorMe();
elem.moveMe("50px");
<div id="example">
  This is an example
</div>
like image 41
Maverick Avatar answered Oct 03 '22 04:10

Maverick