Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Trino Mitra
Trino Mitra
11,165 Points

Using JSColor on Handlebars template?

Hello, I am using JSColor (http://jscolor.com/) to show a color picker. In order to make it work all you have to do is put the class "jscolor" on an input or button element and the picker will show up.

However, this only works for elements that are created when the document initially loads.

My problem is that I want the picker to show up on elements that have been created by a handlebars template, which is not working. Does anyone have any experience dealing with issues similar to this?

2 Answers

rydavim
rydavim
18,813 Points

I haven't used either jscolor or handlebars. However, it sounds like you might be able to just reinitialize jscolor after compiling your handlebars templates?

// Try initializing jscolor after you've handled your templates.
jscolor.init()

That's about as helpful as I can be without seeing your files and doing some more research. If that doesn't work for you, you can post your code here and I can try to fix your specific issue. Good luck!

Trino Mitra
Trino Mitra
11,165 Points

I've tried that but it throws an error saying that jscolor.init() is not a function.

Here's the relevant code:

<script src="jscolor.js"></script>

<!--THIS WORKS BECAUSE IT'S IN THE DOCUMENT-->
Color: <input class="jscolor color" value="ab2567">
<button onclick="addText()">addText</button>

<!--THIS DOESN'T WORK BECAUSE THE TEMPLATE GETS LOADED ON THE PAGE LATER-->
<script type="text/x-handlebars-template" id="text">
    <section id={{order}} class="text">
            Color: <input class="jscolor color" value="ab2567"> 
    </section>
</script>

AND HERE'S THE JAVASCRIPT:

var order=0;
var textsource=$("#text").html();
var textTemplate = Handlebars.compile(textsource);
function addText(){
    order++;
    var text=textTemplate({order:order});
    $creation.append(text);
    /*$creation is where the template gets added*/
}
rydavim
rydavim
18,813 Points

You could try something a bit more robust at the end of your page:

<script type="text/javascript">
    $(document).ready(function() {
        jscolor.init();
    });
</script>

Unfortunately, without being able to see all of your files and structure, I think I don't have enough experience with handlebars to offer specific advice. Sorry!

innitialize your color as an object,save the color object to console log,then use it later by calling the color object:eg if the color object is called colorOB,then use innit colorOB then where you want to display it with a variable,use for instance var (#yourvariable)colourOb.events.html......then use render function to use the colour

$('#myhtm_id_div .myhtm_id').each(function() { $(this).data('event', { var currColor = "#3c8dbc"; myvariable.css({"background-color": currColor, "border-color": currColor, "color": "#fff"}).addClass("myhtm_id");