Results 1 to 5 of 5

Thread: Java Script Timeouts -- Advanced

  1. #1
    Join Date
    Nov 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Script Timeouts -- Advanced

    Hi, i have an onclick, highlight(change background which switches each time a different element is clicked (same element name)

    BUT i would like to implement this on my contacts list

    - one problem this list is updated once a second through ajax here is the javascript:

    var highlightLink = function () {
    var active = null, colour = '#84DFC1';
    if (this.attachEvent) this.attachEvent('onunload', function () {
    active = null;
    });
    return function (element) {
    if ((active != element) && element.style) {
    if (active) active.style.backgroundColor = '';
    element.style.backgroundColor = colour;
    active = element;
    }
    };
    }();


    Is there any way to remenber if there were no clicks and format it the same -- i didnt write this

    there is possibility of the actual <div> tag coming with vars from the backend - i wrote the ajax

    thanks.

  2. #2
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    Here's a slightly improved version of your code:

    Code:
    var highlightLink = (function() {
    	var active = null;
    	var color = "#84dfc1";
    	if(document.addEventListener) document.attachEvent("onunload", function(e){active=null;});
    	return function(el) {
    		if(active) active.style.backgroundColor = "";
    		el.style.backgroundColor = color;
    		active = el;
    	};
    })();
    As for your question, please be more specific. I don't know what you are trying to achieve.
    Trinithis

  3. #3
    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

    Dude, what's up with your title?

    Quote Originally Posted by davo666
    Java Script Timeouts -- Advanced
    There is no timeout in your code, and no mention of one in your post.
    - John
    ________________________

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

  4. #4
    Join Date
    Nov 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    list is updated once a second through ajax
    setInterval timeout...

    and, being more specific, when i click a static (non timed ajax element)
    it stays highlighted, but when i click an updating element it highlight for the rest of the second then goes back white...

    thanks

  5. #5
    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

    I see, so it is a vague (as far as any reasonable reader of this thread so far goes) update/interval/timeout from some other portion of your code. And, at that, sans markup. I can give you some general sorts of answers though, ones that should work, if you can incorporate any of them into your setup. If you were to have, say:

    HTML Code:
    <div id="contact_1">Some data/text/other tags, etc.</div>
    OK, something like that, if your highlight function does something to the object:

    document.getElementById('contact_1')

    by dynamically adding some style (basically like):

    Code:
    document.getElementById('contact_1').style.backgroundColor='white';
    as the code you did give seems to indicate, and your update function replaces it with a similarly structured element with the same id, most browsers will lose the dynamically added style.

    If however, you added another 'layer' to the markup:

    Code:
    <div id="c_1_h"><div id="contact_1">Some data/text/other tags, etc.</div></div>
    You could add your style to "c_1_h" and replace "contact_1". Then the style (if it is relatively simple style, as it appears to be) would be maintained. Alternatively (without adding a 'layer' in the markup), you could replace only the child/children of "contact_1", leaving the division tag alone, that would preserve its highlighting.

    Another way would be to highlight the element, not by adding style to it, but by adding a class designation (className) that has the highlight styles already available to it in the stylesheet. When replacing the element, you would have to check for this class name in the old element and duplicate it for the replacement element, if found.
    - John
    ________________________

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

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
  •