Transform the jQuery UI colorpicker slider into a jQuery Mobile compatible version

I'm not very familiar with JavaScript programming, so I'm feeling a bit lost while trying to understand the code in the jQuery Mobile library (the lack of comments doesn't help). I managed to create a colorpicker slider using jQuery UI, you can view the demo here - Now, I'm seeking assistance in transitioning my code to use jQuery Mobile instead, or any advice on where to get started.

I've noticed that they use a different approach for manipulating elements, so I attempted to adjust them, for example:

    domSlider.setAttribute( "id", "slider-horiz" );
    domSlider.setAttribute( "role", "application" );

I'm unsure about how to integrate the necessary functions into the slider itself, in jQuery UI I was able to do it in my HTML document (as shown in the demo):

    $(function() {
    var box = $('#box')[0];

    $("#slider-horiz").slider({
        orientation: "horizontal",
        min: 0,
        max: 360,
        value: 0,
        slide: function(event, ui) {
            box.style.background = 'hsl(' + ui.value + ', 100%, 50%)';
            var clr = $('#box').css('background-color');
            $('#box').attr('data-color', clr).trigger('click');
            $('#slider-handle').css('background-color', clr);
            if (ui.value == 0) {
                $('#box').attr('data-color', 'hsl(' + ui.value + ', 0%, 0%)').trigger('click');
                $('#slider-handle').css('background-color', '#000');
            }
            if (ui.value == 360) {
                $('#box').attr('data-color', 'hsl(' + ui.value + ', 100%, 100%)').trigger('click');
                $('#slider-handle').css('background-color', '#fff');
            }

        }
    });
}); 

Answer №1

Here is a live example: http://jsfiddle.net/Gajotres/7F9eZ/

Sample HTML :

<!DOCTYPE html>
<html>
    <head>
        <title>jQM Complex Demo</title>
        <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
        <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; minimum-scale=1.0; user-scalable=no; target-densityDpi=device-dpi"/>
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" />
        <script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>    
    </head>
    <body>
        <div data-role="page" id="index">
            <div data-theme="b" data-role="header">
                <h1>Index page</h1>
            </div>

            <div data-role="content">
                <form>
                    <input name="slider-1" id="slider-1" min="0" max="360" value="0" type="range"/>
                    <a href="#colored_sketch" id="box" data-color="" style="border: 1px solid black; width: 30px; height: 30px; background: #000; display: inline-block;"></a>                    
                </form>                                
                <canvas id='colored_sketch' width='800' height='300'></canvas>
            </div>
        </div>    
    </body>
</html>   

Custom Javascript :

$(document).on('pageshow', '#index', function(){ 
    var box = $('#box')[0];
    $('#colored_sketch').sketch();

    $(document).on( "slidestop", "#slider-1", function( event, ui ) {
        box.style.background = 'hsl(' + $(this).val() + ', 100%, 50%)';
        var clr = $('#box').css('background-color');
        $('#box').attr('data-color', clr).trigger('click');        
    });    
});

Styling with CSS:

.ui-slider-track {
    background:url(http://i.imgur.com/lHQxra5.png) repeat-x !important;
    width: 200px !important;
    margin: 0 15px 0 15px !important;
}

#slider-1 {
    display: none;
}

#box {
    width:30px;
    height:30px;
    border:1px solid #000;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Introducing additional choices to the list and automatically refreshing the list with the latest updates

I am currently honing my skills in Yii2 by working on a project using this framework. One of the challenges I am facing is adding new list options dynamically without having to navigate away from the current page. When I click the "Add new option" button ...

Here is an example of how to transfer a value from PHP to a jQuery function in the code snippet provided

This is an example of my code. It is functioning properly even without passing a value. function displayMessage(text) { alert(text); } <button type="button" id="button" class="btn btn-success" onclick="displayMessage("Hello");"> Click Me </ ...

Utilize the Jquery UI feature by seamlessly dragging and dropping items onto tables

I'm currently working on implementing a drag and drop feature that involves moving items from table1 to table2. My goal is to ensure that when the user drags an item from table1, the cell in table2 where it is dropped remains empty. If the cell in tab ...

What's preventing my link from opening?

I've written this code for two tabs: <li> <a href="#gallery_place" role="tab" data-toggle="tab"> <i class="fa fa-picture-o"></i> <?php _e("Gallery", ET_DOMAIN); ?> </a> </li> <li> <a href ...

CSS drop down menu malfunctioning

Trying my hand at anchor tags and drop down menu. I'm encountering an issue in the code below - the dropdown doesn't seem to be working as intended. It should display the text "This is dropdown menu" directly beneath the text "This is text. Its i ...

Rails successfully processed the request with a 200 OK status code, however, the $.ajax() function is triggering the 'error' callback instead of the 'success' callback

In my controller, the ajax request is handled as shown below (simplified): def create ... @answer = Answer.new(video: video_file) if @answer.save render json: @answer.video.url else ... end end This is how I have defined my ajax function: $.aja ...

Showing no background color until the user lifts their finger

I am currently in the process of developing a website. The navigation on the website starts off as transparent (background: transparent) when opened, but as soon as you start scrolling, it should transition to a colorful state with shades like grey, midnig ...

The jQuery Ajax Error is consistently being triggered

I'm puzzled as to why my custom callback error function keeps getting triggered. When I remove this callback function, the success callback works just fine. Some sources online suggest that it could be an encoding issue, but I don't think that&a ...

Conflict of JQuery ajax requests with the Play! framework

Whenever I trigger an action on my page, two ajax calls (A, B) are made to different methods on my server. Most of the time, each request receives its corresponding response. However, occasionally both requests receive the same response! (either A,A or B,B ...

Having trouble making alerts or confirmation dialogs function in JavaScript

EDIT: Many thanks to everyone who helped fix this issue (: Your help is greatly appreciated! I've been struggling to get the alert, confirm, or prompt functions to work properly in my code. Here's a snippet of the code that's causing troubl ...

What is the best way to swap out a div element with a text area when I press a button, all

I recently used a Fiddle found at http://jsfiddle.net/GeJkU/ function divClicked() { var divHtml = $(this).html(); var editableText = $("<textarea />"); editableText.val(divHtml); $(this).replaceWith(editableText) ...

Reloading jQuery event handlers following successful ajax request

I have been working on implementing behavior for DOM objects that are added via AJAX. After reviewing the jQuery API Docs, I believe it would be most effective to incorporate a scoped onInit(scope) handler to handle event binding on the new content. Here ...

Utilizing jQuery Ajax to dynamically update PHP session variables

Although this question may have been answered before, I'm struggling to find a solution that fits my specific issue. I am currently developing a website where users can sell their eBay items using the eBay API, which has proven to be quite challenging ...

Concealing a Div element without the use of Jquery or JavaScript

I have an Upper and Lower div in my HTML code. I am trying to display the Lower div only if the Upper div is present, otherwise hide it. Is there a way to achieve this using CSS without using Jquery or Javascript? Note: No modifications should be made t ...

Determine the number of Fridays that fall between two dates using JavaScript or jQuery

Can you help me calculate the total number of a specific day between two given dates? For instance, if I have a start date and end date stored in a variable, I would like to know how many Fridays fall within that timeframe. ...

In IE8, dynamically loaded CSS does not take effect on dynamically loaded JavaScript views

Concerns have been raised about possibly duplicating this question, especially since it has taken more than an hour to find a solution. The current scenario involves: A widget that requires dynamic loading of CSS Usage of Sammy.js and .ejs for views, wh ...

Unable to access the jumbotron in Angular with Bootstrap 5

Recently, I set up a brand new Angular installation and included Bootstrap 5 using the following command: npm install bootstrap --save After that, I updated the lines in the angular.json file to: "styles": [ "node_modules/bootstra ...

JavaScript - Modify input character prior to appending it to the text area

I am working on creating a virtual keyboard using jQuery. Whenever I press 'a' in the textarea, I want it to display 'z' instead. In my investigation of typing a letter in the textarea, I discovered the following sequence: keyDown ev ...

Change the size of a custom cursor on click using React

I have customized the cursor on my react app, but I want to make it animate when the user clicks. Perhaps by decreasing its size or some other effect. The cursor is within a component that I've included in my Index.js file. I am unsure how to create a ...

Why aren't CSS variables "calculating" the color?

Can anyone help me understand why I am having trouble assigning block-level CSS variables using values from SASS? Specifically, the line color: hsl(3, 75%, var(--main-color-l)); is not producing any output. My setup involves a static site generator with ...