Attribute Default Value

Can you get an attribute default value so you don’t have to repeat it in the following example:

<p title="foo" id="p">Hello, world!</p>
<input type="text" id="i">
<script>
    var p = document.getElementById('p'),
        i = document.getElementById('i');
    i.oninput = function () {
        p.title = this.value;
        if (this.value == 'bar') {
            p.title = 'foo';
        }
    };
</script>

DEMO

Is there something like p.title = p.title.defaultValue as we use for text fields?

I don’t understand. Do you want to change div content by the text in input? So when user types “something” in input you want to change div content to “something” (and if user writes foo, then show bar)?

You mean something like this.defaultValue

or possibly (to set the value of p back to its default value)

p.value = p.defaultValue;

Nope, that’s something that you’ll have to handle for yourself, such as is follows:


function updateTitle(el, text) {
    if (!el.defaultTitle) {
        el.defaultTitle = el.title;
    }
    el.title = text;
}
function resetTitle(el) {
    if (el.defaultTitle) {
        el.title = el.defaultTitle;
    }
}