Header Ads Widget

Responsive Advertisement

Ticker

6/recent/ticker-posts

8.6 HTML AND ITS ELEMENTS.

 ads

 HTML AND ITS ELEMENTS.

ads



Note to self: Create an introduction that helps readers start reading your article rather than jumping right into it.

ads

For Beginners
Let's discuss the difference between HTML and DOM.

ads

A plain old element is clearly HTML. You can use it in files with .html at the end. There are many attributes that affect appearance and behavior.

or more. This has nothing to do with JavaScript.

ads

The DOM connects JavaScript code to HTML elements in your document so you can manipulate these elements just like you would an object.

ads

The document-to-object model.

Each HTML element type has its own DOM "interface" that defines properties (usually mapped to HTML element attributes) and methods. For example,

has an interface called HTMLTableElement.

You can get a reference to a specific element by writing:

ads


You can then access all properties and methods available on that element type. For example, you can use searchBox.value to access the value property or call searchBox to position the cursor in the field.

concentration().ads


Thank you for taking the 58 second course on DOM.

ads

The problem is that most elements don't have interesting methods. So unless you're really browsing the specs for things you rarely use, it's easy to miss the little nuggets scattered here and there. are two of my favorite ways to avoid That's it...

ads

If you want to play along and use the Browser DevTools, select an item in the item tree and type $0 into the console. This will give you a reference to the selected item. Type dir($0) to display the element as an object.

There's a lot you can do with the console.

ads

Photo by Holger Link on Unsplash
By the way, if you want to whet my curiosity, you can highlight any headlines that you don't already know or that seem unhelpful.

ads

Table Method #1
The discreet table (still #1 in website design) has a few nifty ways to make it as easy as making an IKEA table.

There are many here.

ads

I don't see a single document.createElement().

.insertRow() method inserts when called directly on table element
That's a good idea.

ads

#2 scrollIntoView()
If you have #something in the URL, do you know how when the page loads the browser scrolls the page to show the element with that id? Very thoughtful, but rendering this element after the page loads doesn't work.

You can manually reproduce this behavior with:

ads


#3 hidden
OK, it's not a method.

Have you ever created myElement.style.display = 'none' to hide an element anyway? stop!

myElement.hidden = true is allowed.

ads

#4 toggle()
Okay. This is also not an element method. Element property method. Specifically, a method that toggles adding/removing an element's class using myElement.classList.toggle('some-class').

ads

Also, if you ever used an if statement to conditionally add a class, you should be jailed.

You should have passed a second parameter to the toggle method. If true, the class will be added to the element.

ads

I know what you're thinking - that's not really what the word "toggle" means - the good folks at Internet Explorer agree and don't implement the second parameter logic at all I am protesting with

So I take it back, you shouldn't be trapped. Run around freely!

ads

#5 querySelector()
OK, you definitely knew this already, but I think exactly 17% of users didn't know that you can use any element
For example, myElement.querySelector('.my-class') matches only elements that have class my-class and are descendants of myElement.

#6 next()
Method available on all elements to search the element tree. It's like querySelector() in reverse. So you can get the heading of the current section like this:

ads


Go to first

and back to first

.ads

#7 getBoundingClientRect()
This returns a nice little object containing some dimensional details about the calling element.

ads
But please note two things:
A call to
causes a redraw. Depending on the device and site complexity, this may take several milliseconds. So keep that in mind if you call it repeatedly. in animation.
Not all browsers return all of these values. why should they?
#8 matches()
Suppose we want to check if a particular element has a particular class.

Max Complexity:

ads
Better, but unrelated to this post:


Best:
ads

#9 insertAdjacentElement()
I learned it today! You have a little more control over where you do

parentEl.insertAdjacentElement('beforeend', newEl) is the same as parentEl.appendChild(newEl), but you can also specify beforebegin or afterbegin or afterend to place it where those names indicate.

Such control!

So memes.

ads

#10 contains()
Ever wondered if an element is inside another? I've always wanted to know this.

For example, if you're handling a mouse click and want to know whether it happened inside or outside the modal (so you can close it), you can do: e.target is the clicked item.

ads

Fun fact, 100% of the time when you do this you get the logic wrong the first time.

Even if you try to save smartly and flip it over, you still make mistakes.

ads

#11 getAttribute()
Certainly the least useful of all element methods, except for one special case.

Remember how I said earlier that properties are usually mapped to attributes (I used bold instead of italic back then)?

ads
One case where this is not the case is in the href attribute of elements like href="/animals/cat">cat.

ads

el.href does not return /animals/cat as expected. This is because the element implements the HTMLHyperlinkElementUtils interface. This interface has many helper properties that tell you something about the target of the link, such as protocol and hash.

ads

One of these

ads

Note to self: Create an introduction that helps readers start reading your article rather than jumping right into it.

ads

For Beginners
Let's discuss the difference between HTML and DOM.

ads

A plain old element is clearly HTML. You can use it in files with .html at the end. There are many attributes that affect appearance and behavior.

or more. This has nothing to do with JavaScript.

ads

The DOM connects JavaScript code to HTML elements in your document so you can manipulate these elements just like you would an object.

ads

The document-to-object model.

Each HTML element type has its own DOM "interface" that defines properties (usually mapped to HTML element attributes) and methods. For example,

has an interface called HTMLTableElement.

You can get a reference to a specific element by writing:

ads


You can then access all properties and methods available on that element type. For example, you can use searchBox.value to access the value property or call searchBox to position the cursor in the field.

concentration().ads


Thank you for taking the 58 second course on DOM.

ads

The problem is that most elements don't have interesting methods. So unless you're really browsing the specs for things you rarely use, it's easy to miss the little nuggets scattered here and there. are two of my favorite ways to avoid That's it...

ads

If you want to play along and use the Browser DevTools, select an item in the item tree and type $0 into the console. This will give you a reference to the selected item. Type dir($0) to display the element as an object.

There's a lot you can do with the console.

ads

Photo by Holger Link on Unsplash
By the way, if you want to whet my curiosity, you can highlight any headlines that you don't already know or that seem unhelpful.

ads

Table Method #1
The discreet table (still #1 in website design) has a few nifty ways to make it as easy as making an IKEA table.

There are many here.

ads

I don't see a single document.createElement().

.insertRow() method inserts when called directly on table element
That's a good idea.

ads

#2 scrollIntoView()
If you have #something in the URL, do you know how when the page loads the browser scrolls the page to show the element with that id? Very thoughtful, but rendering this element after the page loads doesn't work.

You can manually reproduce this behavior with:

ads


#3 hidden
OK, it's not a method.

Have you ever created myElement.style.display = 'none' to hide an element anyway? stop!

myElement.hidden = true is allowed.

ads

#4 toggle()
Okay. This is also not an element method. Element property method. Specifically, a method that toggles adding/removing an element's class using myElement.classList.toggle('some-class').

ads

Also, if you ever used an if statement to conditionally add a class, you should be jailed.

You should have passed a second parameter to the toggle method. If true, the class will be added to the element.

ads

I know what you're thinking - that's not really what the word "toggle" means - the good folks at Internet Explorer agree and don't implement the second parameter logic at all I am protesting with

So I take it back, you shouldn't be trapped. Run around freely!

ads

#5 querySelector()
OK, you definitely knew this already, but I think exactly 17% of users didn't know that you can use any element
For example, myElement.querySelector('.my-class') matches only elements that have class my-class and are descendants of myElement.

#6 next()
Method available on all elements to search the element tree. It's like querySelector() in reverse. So you can get the heading of the current section like this:

ads


Go to first

and back to first

.ads

#7 getBoundingClientRect()
This returns a nice little object containing some dimensional details about the calling element.

ads
But please note two things:
A call to
causes a redraw. Depending on the device and site complexity, this may take several milliseconds. So keep that in mind if you call it repeatedly. in animation.
Not all browsers return all of these values. why should they?
#8 matches()
Suppose we want to check if a particular element has a particular class.

Max Complexity:

ads
Better, but unrelated to this post:


Best:
ads

#9 insertAdjacentElement()
I learned it today! You have a little more control over where you do

parentEl.insertAdjacentElement('beforeend', newEl) is the same as parentEl.appendChild(newEl), but you can also specify beforebegin or afterbegin or afterend to place it where those names indicate.

Such control!

So memes.

ads

#10 contains()
Ever wondered if an element is inside another? I've always wanted to know this.

For example, if you're handling a mouse click and want to know whether it happened inside or outside the modal (so you can close it), you can do: e.target is the clicked item.

ads

Fun fact, 100% of the time when you do this you get the logic wrong the first time.

Even if you try to save smartly and flip it over, you still make mistakes.

ads

#11 getAttribute()
Certainly the least useful of all element methods, except for one special case.

Remember how I said earlier that properties are usually mapped to attributes (I used bold instead of italic back then)?

ads

ads

el.href does not return /animals/cat as expected. This is because the element implements the HTMLHyperlinkElementUtils interface. This interface has many helper properties that tell you something about the target of the link, such as protocol and hash.

ads

One of these

Post a Comment

0 Comments