programming.name Posted December 20, 2012 Share Posted December 20, 2012 Hi, there. Is that bad practice not to have tag Ids except when needed? What about name and class? Are they necessary, and what happen if they don't appear Thanks in advance Quote Link to comment Share on other sites More sharing options...
codefossa Posted December 20, 2012 Share Posted December 20, 2012 You use them for styling and easy access in Javascript. The names are for form items you need sent as GET or POST parameters. If you don't need it, then there's no reason to put it there. Quote Link to comment Share on other sites More sharing options...
Love2c0de Posted December 22, 2012 Share Posted December 22, 2012 Just to note you can only have one id per page. For instance, you can't declare 2 <div> tags and give them both the same ID. That kind of thing you would use a class for I believe. Quote Link to comment Share on other sites More sharing options...
haku Posted January 6, 2013 Share Posted January 6, 2013 (edited) Is that bad practice not to have tag Ids except when needed It's not bad practice at all. On the contrary, adding IDs to everything would clutter up your HTML and make it more difficult to read. I personally only add IDs if I know for a fact I need them, or if there is a strong liklihood that I (or more often my front-end developer) will use them in the future. What about name and class? Classes are not needed for every element either, and generally should follow the same rules I gave for IDs above. Names are only for form elements, and are required for for elements if you want to be able to access the values upon submission (which, you generally would, or else you wouldn't need a form element). Edited January 6, 2013 by haku Quote Link to comment Share on other sites More sharing options...
jardrake Posted January 7, 2013 Share Posted January 7, 2013 I would say that adding ids to each element is an excellent practice. It will save you a lot of work if you decide to implement some Javascript later. I honestly can't think of a situation where you couldn't create unique ids for each element. Quote Link to comment Share on other sites More sharing options...
cpd Posted January 7, 2013 Share Posted January 7, 2013 I can think of plenty. Normally, you would assign ID's to wrappers and sub wrappers. For example you could give an ID to your "header", "body" and "footer" to easily identify them. You can then give an ID to your "navigation" wrapper (sub wrapper). An example of something you may not bother giving an ID to is a ul tag and possibly li tags inside your navigation - this is obviously dependent on how your navigation works, though. That's the rule of thumb I work on. Quote Link to comment Share on other sites More sharing options...
haku Posted January 7, 2013 Share Posted January 7, 2013 Since IDs need to be unique, I generally only apply them to elements that may appear multiple times on a page, but need to be uniquely identified/isolated from other elements of the same type. For example, there are multiple divs on a page, but the header and footer will be unique so they get IDs. I wouldn't add a unique ID to every div on the page, because for example some are there simply as innertubes, to ensure cross-browser compatibility. Giving each of these IDs would only add bloat to the page, as these elmements would never need to be uniquely identified. I do however give these inner tubes a common class name. Quote Link to comment Share on other sites More sharing options...
Love2c0de Posted January 7, 2013 Share Posted January 7, 2013 Since IDs need to be unique, I generally only apply them to elements that may appear multiple times on a page, but need to be uniquely identified/isolated from other elements of the same type. You can only apply id's to a single element on any given page. For what you said there, if you have multiple div's which have the same styles (i presume so because you said you were using the id multiple times), you should be giving them a class. It works in pretty much the same way. For example, I query products from a database, for every row that is found, it prints a div with the information inside for each individual product. I have styled the div's identical so I decided to give the <div> a class when writing the php. Not sure if you're bothered about this but it won't pass validation neither. Regards, L2c Quote Link to comment Share on other sites More sharing options...
codefossa Posted January 7, 2013 Share Posted January 7, 2013 No, he was saying that if he had a bunch of div's, he would give whichever he needed to uniquely identify an ID (their own ID). That way he could work with that one individually from the rest easily. He wasn't saying to give them all the same ID. Quote Link to comment Share on other sites More sharing options...
haku Posted January 8, 2013 Share Posted January 8, 2013 You can only apply id's to a single element on any given page. For what you said there, if you have multiple div's which have the same styles (i presume so because you said you were using the id multiple times), you should be giving them a class. It works in pretty much the same way. Xaotique already replied for me, but to elaborate: Since IDs need to be unique, I generally only apply them to elements that may appear multiple times on a page, but need to be uniquely identified/isolated from other elements of the same type. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.