Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it alright to add custom Html attributes?

Tags:

html

jquery

css

I have a site I'm working on where I want to mark if a row of data has been changed. If something has been changed in that row I would mark it with a custom attribute like so.

<tr>
  <td isDirty="true">
     ....row data
  <td>
</tr>

This works great with jQuery and it doesn't add to much code to my page.

But is this really the correct way of doing something like this and what are the downsides?

I guess another way of doing this would be like this, but it seems like over kill.

<tr>
  <td>
     ....row data
     <input id="isDirty" type="hidden" value="true" />
  <td>
</tr>
like image 586
Donny V. Avatar asked Jan 13 '09 14:01

Donny V.


People also ask

Can you use custom HTML attributes?

Custom attributes are attributes that are not part of the standard HTML5 attributes but are explicitly created. They allow us to add our own information to HTML tags. These are not specific and can be used with all the HTML elements.

Are custom HTML tags valid?

Custom tags are not valid in HTML5. But currently browsers are supporting to parse them and also you can use them using css. So if you want to use custom tags for current browsers then you can. But the support may be taken away once the browsers implement W3C standards strictly for parsing the HTML content.

What is the recommended way to add custom HTML5 attributes?

You can use getAttribute() and setAttribute() in JavaScript to get and set the value of different data attributes. The getAttribute method will either return null or an empty string if the given attribute does not exist. Here is an example of using these methods: var restaurant = document.

What is the purpose of adding attributes in HTML tags?

HTML attributes can be used to change the color, size, and other features of HTML elements. For example, you can use an attribute to change the color or size of a font for a text element or the width and height for an image element.


2 Answers

Why don't you use the jQuery data capability?

Your example will be (I don't know the condition to select proper td):

$("tr td").data("isDirty", true); 

take a look at documentation

like image 75
tanathos Avatar answered Oct 20 '22 06:10

tanathos


Technically you should be using the class attribute for this. Tags can have more than one class so it shouldn't affect anything

<td class="isDirty otherclass">
like image 29
AAA Avatar answered Oct 20 '22 06:10

AAA