Forum Moderators: open

Message Too Old, No Replies

Attribute Default Value

         

Rain_Lover

7:00 am on Jul 23, 2014 (gmt 0)

10+ Year Member Top Contributors Of The Month



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 [jsfiddle.net]

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

Fotiman

1:10 pm on Jul 23, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



No, but you could store the attribute's default value in a data attribute.

<p title="foo" id="p" data-title="foo">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 = p.getAttribute('data-title');
}
};
</script>