Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

HTML + JS add attribute into a tag?

This question might be stupid. But i will be so happy if we could solve it.

consider the following html line :

<input name="test" id="test" type="text" />

can i put a new attribute into that html element so the line will looks like this?:

<input name="test" id="test" type="text" newattribute="new_value" />

i just wanted to know if it is possible to do that, and if so, how can it be done?

any help would be appreciated....

Update:

I've found the answer. To share with the reader - we can do that, and to get the value of newattribute, we could use .getAttribute("newattribute");

btw, i wanted to star the first answer but i lack my point....

Update 2:

Well, to respond to the second answerer, I understand that you are trying to pull up some CSS and JAVASCRIPT technique. But this isn't what i want. For me, the ability of the tag to hold an additional attribute and able to be accessed is enough to solve the problem. It does not matter on either HTML will do anything with it or not.

2 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    Use the "setAttribute()" method for Element objects in the DOM

    http://www.javascriptkit.com/domref/elementmethods...

    However in HTML you should really only create attributes that are recognised as standard (e.g. recommended by W3C) - custom attributes are the domain of XML

  • Anonymous
    1 decade ago

    Although you could simply add an arbitrary attribute to an HTML document, the browsers won't know what that attribute means, so they won't do anything with it.

    However, if what you really want to do is add some sort of marker to a tag so you can do more interesting things with it later, you can add a class or id to it.

    For example:

    <p id = "output">

    This is my paragraph

    </p>

    is an ordinary paragraph, but because you added the id tag to it, you could do this in the CSS:

    p.output {

    color: blue;

    }

    You can also use JavaScript to modify any element that you've added an id to, like this:

    <script type = "text/javascript">

    p = document.getElementById("p");

    p.innerHTML = "The text changed";

    </script>

    You can also add a class attribute to any element. IDs are unique, but you can apply a class to many elements on the same page, and each element can have more than one class. In this way, you can use css to generate a bunch of different characteristics, and then just apply the classes you want to your HTML elements.

    Hope this helps. Stop by the site if you want more examples or to ask another question...

    Source(s): HTML / XHTML / CSS All in One for Dummies (author) JavaScript / AJAX for Dummies (author) http://www.aharrisbooks.net/
Still have questions? Get your answers by asking now.