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" );
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.
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.
You can name each function according to the div 's id and call it dynamically using the object["methodName"]() syntax to call it.
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.
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.
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.
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With