0

I need some help with a js code. I've searched in other topics and i've tried some codes, but they didn't work. I need to change 2 text every 3 seconds and also the second text has to change their colour.

I used an jquery code, and the first part is done, but i don't know how to do the second.

This is my code

<html>
<head>
<script src="js/jquery-2.1.4.min.js" type="text/javascript"></script>
<script>
$(function () {
    cuenta = 0;
    txtArray = ["TEXT1", "TEXT2"];
    setInterval (function () {
        cuenta++;
        $("#titulos").fadeOut(100, function () {
            $(this).text(txtArray [cuenta % txtArray.length]).fadeIn(100);
        });
    }, 3000);
});

</script>
<body>
<div>
<p id="titulos">TEXT1</p>
</div>
</body>
</html>

How can I change the colour of the second array? Is there any other way to make this code with pure js, not with jquery?

Thanks for all

2
  • If we refuse to answer, will we save ourselves from bad website designs? Commented Nov 11, 2015 at 13:22
  • What's the point of not using jQuery when you already included it? Yes you can do this is native JavaScript, but will be more complex to code. Commented Nov 11, 2015 at 13:24

4 Answers 4

1

To change CSS with jQuery use .css('rule-name', 'value') or .css({rule1: 'value', rule2: 'value'})

  $(function() {
    cuenta = 0;
    txtArray = ["TEXT1", "TEXT2"];
    setInterval(function() {
      cuenta++;
      $("#titulos").fadeOut(100, function() {
        $(this)
          .text(txtArray[cuenta % txtArray.length])
          .css('color', cuenta % 2 == 0 ? 'red' : 'blue')
          .fadeIn(100);
      });
    }, 3000);
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <p id="titulos">TEXT1</p>
</div>

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

Comments

1

try this

https://jsfiddle.net/33mbjjym/

var colors = ['blue', 'red', 'green', 'pink', 'yellow'];
var colorIndex = 1;
setInterval(function(){
    $('#titulos').css('color', colors[colorIndex]);
    if(colorIndex < colors.length)
        colorIndex += 1;
    else
        colorIndex = 1;
}, 3000);

2 Comments

not sure how this translates if you want to exclude jQuery
It actually is only a single line change in your code: Here is a pure JS version, just note you don't have any fading effects.
0

Here you go:

$(function () {
    cuenta = 0;
    txtArray = ["TEXT1", "TEXT2"];
    setInterval (function () {
        cuenta++;
        $("#titulos").fadeOut(100, function () {
            $(this).text(txtArray [cuenta % txtArray.length]).fadeIn(100);
            if(cuenta % txtArray.length == 1)
            {
                $(this).css("color","red");
            }
            else
             {
                $(this).css("color","black");
            }   
        });
    }, 3000);
});

1 Comment

Thanks for all the answers and recommendations! You guys are the best!!
0

Since you are using JQuery, you can use the css method to change the color like this:

$(this).css("color", "red");

As an offtopic and personal recommendation, I'd suggest you to name your variables in English :).

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.