views:

170

answers:

2

I'm writing an auction template for eBay, hoping that eBay would allow it. Apparently they don't because jquery has things like string.replace() etc.

The code is very basic.

$(document).ready(function(){

    function changeImage(){
        if($("#coin1").css("display") == "none"){  
            $("#coin1").fadeIn("slow");
        }else{  
            $("#coin1").fadeOut("slow");
        }
    };

    setInterval ( changeImage, 5000 );
});

I basically need to rewrite it in plain Javascript...

+3  A: 

If you can live without the fading effect, it should be pretty straightforward:

function changeImage() {
    var image = document.getElementById('coin1');
    image.style.display = (image.style.display == 'none') ? 'block' : 'none';
}

setInterval(changeImage, 5000);

While fading is cool and all, it's really makes the code a lot more complicated, when we not allowed to use external libraries. Basically, you will need to deal with additional timers, firing with very short intervals, changing the opacity of the target element on each callback.

If you really want fading, see "Javascript Tutorial - Simple Fade Animation".

Jørn Schou-Rode
Thanks! I'm sort of looking to go for the fade effect though...
Jared
eBay doesn't allow scripts that can replace strings modify advanced settings - no I am not allowed to link externally either, but even if you paste it directly in your code, it doesn't work.
Jared
I've updated my answer with more info about fading, including a link to a simple implementation in "plain" JavaScript.
Jørn Schou-Rode
Thanks that was the ticket!
Jared
guess what, four years ago we all did somehow manage to write code with timers, setting visibility, opacity and whatnot, not having jQuery as such! ;-)
naivists
+1  A: 

The most basic of fading, not cross-browser compatible (try in Chrome):

<div id="x" style="background-color: yellow;">
    <a href="#" onclick="fade();">fade me out</a>
</div>

<script type="text/javascript">
    var val = 1.0;
    function fade()
    {
        document.getElementById('x').style.opacity = val;
        val -= 0.1;

        if (val != 0)
        {
            setInterval(fade, 100);
        }
    }
</script>
Langdon