Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi Team

I have created div that has body, but i want to use color inside in case it gets created without a user choose theirs. But mine is not working well and there is no error when i debug, need some help here thanks.

What I have tried:

HTML
<pre> <div class="card" style="width: 18rem;">
                                    <div class="jump-response"></div>
                                    <div class="card-body d-flex flex-1">
                                        <a href="#" class="btn btn-primary pull-left">Published</a>
                                    </div>

                                    <div class="card-body">
                                        <h4 class="card-title">John Doe</h4>
                                        <p class="card-text">Some example text.</p>

                                    </div>
                                </div>



<script type="text/javascript" src="~/Scripts/bootstrap.min.js">
   function get_random_color() {
    var letters = '0123456789ABCDEF'.split('');
    var color = '#';
    for (var i = 0; i < 6; i++ ) {
        color += letters[Math.round(Math.random() * 15)];
    }
    return color;
}
    $(function() {
    $(".jump-response").each(function() {
        $(this).css("background-color", get_random_color());
    });
});
</script>
Posted
Updated 8-Sep-20 23:24pm

1 solution

You're setting the background-color on an empty <div>. The colour will be updated, but it won't be visible.

Either fix the markup so that the .jump-response element is not empty, or change the Javascript to set the colour on the correct element.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900