Welcome to WebmasterWorld Guest from 54.167.216.93

Forum Moderators: open

Message Too Old, No Replies

Attribute Default Value

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

5+ 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?
1:10 pm on Jul 23, 2014 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ 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>
 

Featured Threads

Hot Threads This Week

Hot Threads This Month