0

I am trying to colorize a string/word in HTML. I found a response here - but I am having trouble understanding how to convert to use the function more than once..

http://jsfiddle.net/8VDm4/

<div id="arch" style="font-size: 40px">First Word work fine</div>
<div id="arch" style="font-size: 40px">Second time - does not work</div>



var colours = ["#635636", "#FF00C0", "#990066", "#FF9966", "#996666", "#00FF00", "#CC9933"], 
idx;

$(function() {
var div = $('#arch'); 
var chars = div.text().split('');
div.html('');     
for(var i=0; i<chars.length; i++) {
    idx = Math.floor(Math.random() * colours.length);
    var span = $('<span>' + chars[i] + '</span>').css("color", colours[idx]);
    div.append(span);
}
});

How to create a function that I can call multiple times in a HTML?

1
  • 2
    you can only have one element with a particular id Commented May 6, 2014 at 17:55

3 Answers 3

1

You can only have one element with a particular id. Replace your id with class (or add a particular class to elements) like follows

<div class="arch" style="font-size: 40px">First Word work fine</div>
<div class="arch" style="font-size: 40px">Second time - does not work</div> 

javascript

//code has been update in response of observation made by adeneo, thanks adeneo

var colours = ["#635636", "#FF00C0", "#990066", "#FF9966", "#996666", "#00FF00", "#CC9933"],
    idx;

$(function () {
    $('.arch').each(function () {
        var div = $(this),
            chars = div.text().split(''),
            span = '';

        div.html('');
        for (var i = 0; i < chars.length; i++) {
            idx = Math.floor(Math.random() * colours.length);
            span = span + $('<div>').append($('<span>' + chars[i] + '</span>').css('color', colours[idx])).html();
        }
        div.append(span);
    });
});

I've added some code to avoid frequent update of DOM

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

8 Comments

That surely can't work, div.text() and div.html() would work on both elements at the same time ?
I tried with making minimum changes to the code (replace id with class) and it worked (in firefox. checking in other browsers)
update: works in google chrome and internet explorer too, so probably jQuery is doing some magic or identifying the collection and working on one element at a time?
I don't believe you, show me it working in a fiddle (tip: you can borrow Jay's fiddle, it has the same errors.)
please believe me :D I see colorized strings in jay's fiddle too
|
0

Change the id's on your div's to classes -

<div class="arch" style="font-size: 40px">First Word work fine</div>
<div class="arch" style="font-size: 40px">Second time - does not work</div>

And then change your selector in your jQuery -

var div = $('.arch'); 

http://jsfiddle.net/8VDm4/2/

Comments

0

As Ejay has mentioned, it doesn't work because you used ID's. You can only use an ID once on a page. What you want to use are classes.

Markup:

<div class="arch">You content</div>

Javascript:

var div = $('.arch');

I've also updated the JSFiddle: http://jsfiddle.net/8VDm4/1/

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.