0

I'm creating an presentation. There is a div element which does change the font-size a lot. There are two buttons one should increase the color in the array and the other one should decrease it.

html:

 <input type="button" id="up" value="change color up">
    <input type="button" id="down" value="change color down">

    <div id="myValue">
   VALUE 
    </div>

Jquery:

     fancyColors = {
            1: "#9c9e9f",
            2: "#848e6f",
            3: "#778861",
            4: "#7da75d",
            5: "#7fa433",
            6: "#97bf0d"
        };


   $(function () { 

        var i;
        var valuE = $('#myValue');
        var getSize = $('#myValue').css("font-size");
        var getColor = $('#myValue').css("color");
        var down = $('#down');
        var up = $('#up');



        valuE.css("color", fancyColors[1]);
        down.on("click",function() {
            valuE.css("color", fancyColors[i]); // do i--
        });  


        up.on("click", function() {
            valuE.css("color", fancyColors[i]); // do i++
        });  
    });

I had a for loop but that didn't work out for me. I guess it should be pretty easy for a more experienced person. Thanks for your time.

7
  • fancyColors is not an array. Commented Mar 31, 2014 at 7:25
  • 1
    $('#up') won't work because up is not id. Same goes for down. Commented Mar 31, 2014 at 7:26
  • @Satpal: your argument is invalid :P Commented Mar 31, 2014 at 7:27
  • @Satpal What is it than? Commented Mar 31, 2014 at 7:27
  • @auL5agoi - It's an object, but the keys are numbers, so still works. Commented Mar 31, 2014 at 7:28

5 Answers 5

2

Try like this

fancyColors = [
     "#9c9e9f",
     "#848e6f",
     "#778861",
     "#7da75d",
     "#7fa433",
     "#97bf0d"
 ];
 var index = 0;
 $("#up").click(function () {
     index++;

     var i = fancyColors.length % index;
     $("#myValue").css("background-color", fancyColors[i]);
 });
 $("#down").click(function () {
     index--;
     var i = fancyColors.length % index;

     $("#myValue").css("background-color", fancyColors[i]);
 });

Demo

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

Comments

0

Make your html as

<input type="button" id="up" value="change color up">
<input type="button" id="down" value="change color down">
<div id="myValue">VALUE</div>

And your js:

var fancyColors = [
    "#9c9e9f",
    "#848e6f",
    "#778861",
    "#7da75d",
    "#7fa433",
    "#97bf0d"];


$(function () {

    var i = 0;
    var valuE = $('#myValue');
    var getSize = valuE.css("font-size");
    var getColor = valuE.css("color");
    var down = $('#down');
    var up = $('#up');



    valuE.css("color", fancyColors[1]);
    down.on("click", function () {
        valuE.css("color", fancyColors[i]);
        i++ // do i--
    });


    up.on("click", function () {
        valuE.css("color", fancyColors[i]);
        i++ // do i++
    });
});

Live Demo: http://jsfiddle.net/jY24d/

1 Comment

Welcome. Its My pleasure ;)
0

You're a student, right? Well I won't do your homework for you, but I'll give you the following tips:

  1. The buttons need ids
  2. The iterator (i) needs a value before the function first executes
  3. You can't define the click events to call a function inside the function itself

But I'm sure that's in your lecture materials anyway...

Comments

0
 fancyColors = {
                1: "#9c9e9f",
                2: "#848e6f",
                3: "#778861",
                4: "#7da75d",
                5: "#7fa433",
                6: "#97bf0d"
            };

   $(function () { 

        window.colorIndex=1;
        var valuE = $('#myValue');
        var getSize = $('#myValue').css("font-size");
        var getColor = $('#myValue').css("color");
        var down = $('#down');
        var up = $('#up');
        valuE.css("color", fancyColors[1]);
        down.on("click",function() {

        if(colorIndex<1){colorIndex=6;}
            valuE.css("color", fancyColors[--colorIndex]); // do i--
        });  


        up.on("click", function() {

       if(colorIndex >6){colorIndex=1;}            
            valuE.css("color", fancyColors[colorIndex++]); // do i++
        });  
    });

Try this :)

Comments

0
 $('#down').click( function(){ i++; });

You need to add an id attribute to your down input :

 <input type='button' id='down'>

That jQuery will match correctly with #down

3 Comments

I didn't add it here but in my source-code it's correct. sorry for mistake.
Could you please paste the complete code in. I might have missed something. Thanks
@auL5agoi, anoop did it for me :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.