0

Here is my code:

    <script>
window.addEvent('domready', function(){

    new Request.Stocks({
        stocks: ['SXCL'],
        onComplete: function(yahoo){
            var result = '';
            Array.each(Array.from(yahoo.query.results.quote), function(quote){
result += '<div class="company-ticks"></div>
<span class="company">Steel Excel ({Name})</span>
<span class="sub-info"> - OTC Markets<span> </div>  <div><span class="value">
{LastTradePriceOnly}</span><span class="changeup">
<img src="change-up.gif" class="change-img" />{Change} 
({ChangeinPercent})</span></div></div>'.substitute(quote);
            }, this);

            $('stocks').set('html', result);
        },
        onRequest: function(script){
            $('stocks').set('text', 'Loading...');
        }
    }).send();

    // Request.Stocks.element.js


});

</script>

You see where I have the variable {Change]. I need to determine if this variable is a positive or negative value. If it is positive then it should display the class as "changeup" and the image as change-up.gif. If the value is negative then the class displayed should be "changedown" and the image would be change-down.gif

The images are a green arrow up and a red arrow down. The classes make the color altrenate between red and green.

Because this is within an array thats being called using a function I'm not sure how to go about this. I assume I would have to split up my "result" into 3 sections. The section before, the section that sets the class and the image, and then the rest of the result.

Any help would be appreciated.

This uses Javascript with mooTools. It's pulling a stock quote from yahoo.

1 Answer 1

2

I made the assumption that the Change variable was a property of the quote object. Otherwise that's an easy fix in the code bellow.

Array.each(Array.from(yahoo.query.results.quote), function (quote) {
    quote.changeImage = (quote.Change > 0) ? 'change-up.gif' : 'change-down.gif';

    result += '<div class="company-ticks"></div>
<span class="company">Steel Excel ({Name})</span>
<span class="sub-info"> - OTC Markets<span> </div>  <div><span class="value">
{LastTradePriceOnly}</span><span class="changeup">
<img src="{changeImage}" class="change-img" />{Change} 
({ChangeinPercent})</span></div></div>'.substitute(quote);
}, this);

Please note, producing HTML in the was you are doing is a bit risky and hard to maintain.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.