Results 1 to 2 of 2

Thread: Attribute Default Value

  1. #1
    Join Date
    Nov 2009
    Location
    Isfahan, Iran
    Posts
    229
    Thanks
    46
    Thanked 1 Time in 1 Post

    Default Attribute Default Value

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

    Code:
    <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?

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Nope. You could save the value then recall it later. Also, as a point of information, since p.title is a string, it can only have the prototypical properties and functions of a string, like length, anchor(), etc. You cannot create a property for it - say p.title.defaultValue, like you could if it were an object. You could store its beginning value in another attribute for the element because the element is an object. HTML 5 is the most standard for doing something like that:

    Code:
    <p title="foo" id="p">Hello, world!</p>
    <script>
    	var p = document.getElementById('p');
    	p.setAttribute('data-beginningTitle', p.title);
    </script>
    Once you have that much, you can access the original title of p anytime by:

    Code:
    p.getAttribute('data-beginningTitle');
    Resetting it to its beginning value would be done like so:

    Code:
    p.title = p.getAttribute('data-beginningTitle');
    But you could just as easily save the original value of p.title to a global variable or a property of some other object, or as a variable in some limited yet sufficient scope to achieve retrieval as needed.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    Rain Lover (07-24-2014)

Similar Threads

  1. Set the bgcolor attribute value
    By Rain Lover in forum JavaScript
    Replies: 14
    Last Post: 07-27-2011, 02:41 PM
  2. The rel attribute and scripting.
    By jlizarraga in forum JavaScript
    Replies: 1
    Last Post: 05-19-2009, 01:02 AM
  3. 'for' attribute
    By hmsnacker123 in forum The lounge
    Replies: 9
    Last Post: 04-13-2008, 11:52 PM
  4. Target attribute
    By Titties in forum HTML
    Replies: 19
    Last Post: 08-22-2006, 08:41 PM
  5. Changing an attribute.
    By droide in forum JavaScript
    Replies: 5
    Last Post: 07-13-2006, 03:43 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •