how to create Runtime html tag attribute using javascript?
how to create Runtime html tag attribute using javascript?
i have create select attribute ( multiple ) at runtime ! it possible ?
First of all, runtime is an imprecise term as regards both javascript and HTML. Do you mean onload, or as the page is being parsed?
If the latter, the easiest way is of course to hard code the attribute to the select tag.
There are at least several other options, here are two -
As the page is parsed:
Onload:Code:<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <select id="myselect"> <option>Something</option> <option>Something</option> <option>Something</option> </select><script type="text/javascript"> document.getElementById('myselect').multiple = true; </script></body> </html>
Code:<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><script type="text/javascript"> onload = function(){ document.getElementById('myselect').multiple = true; } </script></head> <body> <select id="myselect"> <option>Something</option> <option>Something</option> <option>Something</option> </select> </body> </html>
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
Just a quick follow up question John, is the value of true for multiple attribute valid when doing like that (el.attribute=value)?
I thought it was only multiple that's valid.
http://www.w3.org/TR/html401/interact/forms.html#h-17.6
Hope to hear some of your thoughts.
Learn how to code at 02geek
The more you learn, the more you'll realize there's much more to learn
Ray.ph!
From the link you posted (emphasis added):
In case you didn't know, boolean means true or false. However, I believe you are correct as far as valid HTML/XHTML goes. But since this is javascript, things are different. The bottom line is that the browser will set the valid HTML for the DTD of the page visa vis its own requirements for it once javascript sets the boolean value.multiple [CI]
If set, this boolean attribute allows multiple selections. If not set, the SELECT element only permits single selections.
To think of it another way, what I did wasn't exactly setting the attribute value of the tag. I set the boolean value of the property of the object. This has the same ultimate result in any compliant browser. If I had quoted 'true', it would have been invalid unless error corrected.
Last edited by jscheuer1; 01-09-2009 at 12:43 PM. Reason: add info
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
rangana (01-10-2009)
Okay, that makes sense. Thanks for clearing it up. Will take this from you:
Originally Posted by jscheuer1
Learn how to code at 02geek
The more you learn, the more you'll realize there's much more to learn
Ray.ph!
No, it would have the same effect — it would be type-coerced. It's only 'false' you've got to look out for, since it's truthy.If I had quoted 'true', it would have been invalid unless error corrected.
Not to repeat what John said, but maybe to shed light on it from a different angle: the attribute on the element is boolean (true or false) but HTML doesn't have boolean datatypes, so instead of writingmultiple=trueandmultiple=falsein HTML, we writemultiple="multiple"ormultiple="". In fact, that is mostly for XHTML compatibility: in HTML we would normally use shorthand, just writingmultipleor else leaving the attribute out entirely.
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!
rangana (01-10-2009)
Thank you Twey. That makes a lot more sense.
Learn how to code at 02geek
The more you learn, the more you'll realize there's much more to learn
Ray.ph!
In my view, type coercion is a form of error correction. And just to be clear, though I'm pretty sure everyone immediately involved already gets this, false (unquoted) is fine. It's just that once it it is quoted, it becomes a string. Any non-empty string will in this situation be type coerced into a value of true.
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
Bookmarks