Jump to content

user data in HTML


Kennys

Recommended Posts

Hi,I'm building an (X)HTML application which uses a lot of JavaScript.One of the things I want to do is to shown/hide specific <tr> elements using JavaScript. How to do that is not the problem.But to decide which rows to show I need some attribute in the <tr> element.I've figured out 2 idea's, both not very ideal I think:1)<table> <tr id="a">...</tr> <tr id="a">...</tr> <tr id="b">...</tr> ...</table>Now hide all <tr>s with id="a" (for example).Note: id's are not unique now (and not intended for this purpose?).2)<table> <tr><!-- id="a" -->...</tr> <tr><!-- id="a" -->...</tr> <tr><!-- id="b" -->...</tr> ...</table>In JavaScript I can reach the comment via innerHTML to decide whether to show the <tr> or not.Which method is the "prefered" one? Is there a better way?Martijn

Link to comment
Share on other sites

Put the value of document.getElementByTagName("tr") in a variable called x(or anything that you fancy. :)) Then do a for loop that checks until i is equal to x. In that for loop, do getElementByTagName("tr").innerhtml and check it if it's equal to what you want. Is that what you want?

Link to comment
Share on other sites

Put the value of document.getElementByTagName("tr") in a variable called x(or anything that you fancy. :)) Then do a for loop that checks until i is equal to x. In that for loop, do getElementByTagName("tr").innerhtml and check it if it's equal to what you want. Is that what you want?

Not really I think. This is basically my 2nd solution.Based on some criterion I want to show 0, 1, or some of the <tr>s.The criterion is something which should not show on the screen, but is part of the <tr>.In my 2nd solution I indeed do use innerHTML to get the string"<!-- id="a" -->...."Then I extract with some substring-guru-stuff the "a" or "b" to decide whether to show or hide the <tr>. But this depends on the exact layout of the comment.What I want to do is something like:<table><tr userdata="a">...</tr><tr userdata="a">...</tr><tr userdata="b">...</tr>...</table>But thats not HTML :( The nearest to that is using the id attribute, see solution 1).But that's (I think) mis-using the id attribute.So I wonder if there's a general way to add some user-defined attribute to an element (any element, not just <tr>, btw) that does not influence HTML in any way, but that can be extracted in JavaScript using the DOM...The Dutch word 'dom' means 'stupid', btw :) Thanks for bearing with me....Martijn
Link to comment
Share on other sites

to my knowledge, there are no user defined attributes in XHTML. But "id" is a standard attribute for any tag - so I do not see why that would be a problem using it. So I would say that method (1) is the way to go.<tr> tag details:http://www.w3schools.com/tags/tag_tr.asp

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...