jquery- change element background color by id using elementFromPoint

DavSev picture DavSev · Jun 29, 2015 · Viewed 11k times · Source

I have a grid, I want to imitate the drawing function that was made in this link, the thing is that the functions on that code do not work on mobile devices.

I managed to get the id if the square I'm touchmove through in the console. for some reason i can't change the square background-color i touchmove through.

this is the code I am using to change the background color:

         var elementTouching = document.elementFromPoint( 
               distx, //x coordinate
               disty  //y coordinate
            );
    console.log(elementTouching.id); // check that the id does appear in the console


//i think that the problem is in the next row
    $(elementTouching.id).css({"background-color":"blue"}); 

this is the entire code:

css

.b{
    width: 50px; 
    height: 50px; 
    display: inline-block;
    border: red 1px solid;
}

html

<div id="demo"></div>
<h3 id="statusdiv">Status x</h3>
<h3 id="statusdiv1">Status y</h3>

js

                            createLoop();
            $('.b').bind('touchstart', StartDragSelect);
            $('.b').bind('touchstart', successA);



        function createLoop() {
            var length = 5;
            var text = "";
            var demo = $("#demo")
            for ( i = 0; i < length; i++) {
                var rowElement = $('<div class="a"></div>');
                demo.append(rowElement);
                for (var x = 0; x < length; x++) {
                    createGridItem(rowElement, i, x);
                }
            }
        }

        function createGridItem(rootElement, i, x) {
            var pix = 10;
            var currItem = $('<div class="b" id="a' + i + x + '" style="top:' + i * pix + 'px; left: ' + x * pix + 'px;  background-position-x: -' + x * pix + 'px ; background-position-y:-' + i * pix + 'px";"></div>');
            $(rootElement).append(currItem);
        }

        function StartDragSelect(obj, elementTouching) {

            var id = obj.currentTarget.id;
            obj = obj.currentTarget;

            console.log(id);
            console.log(obj);
            console.log(elementTouching)
            $(elementTouching.id).css({
                "background-color" : "blue"
            });
        }

        function successA() {
            var v1 = document.getElementById('a11');

            if (v1.style.backgroundColor == "blue") {
                alert("Hello! I am an alert box!");
            }
        }

        /**/

         window.addEventListener('load', function(){

         var demo = document.getElementById('demo')
         var statusdiv = document.getElementById('statusdiv')
         var statusdiv1 = document.getElementById('statusdiv1')
         var startx = 0
         var starty = 0
         var distx = 0
         var disty = 0

         demo.addEventListener('touchstart', function(e){
         var touchobj = e.changedTouches[0] // reference first touch point (ie: first finger)
         startx = parseInt(touchobj.clientX) // get x position of touch point relative to left edge of browser
         starty = parseInt(touchobj.clientY) // get y position of touch point relative to left edge of browser
         statusdiv.innerHTML = 'Status: touchstart<br> ClientX: ' +'-'+startx + 'px'
         statusdiv1.innerHTML = 'Status: touchstart<br> ClientX: ' + starty + 'px'
         e.preventDefault()
         }, false)

         demo.addEventListener('touchmove', function(e){
         var touchobj = e.changedTouches[0] // reference first touch point for this event
         var distx = parseInt(touchobj.clientX)
         var disty = parseInt(touchobj.clientY)
         statusdiv.innerHTML = 'Status: touchmove<br> Horizontal distance traveled: ' +'-'+distx + 'px'
         statusdiv1.innerHTML = 'Status: touchmove<br> vertical distance traveled: ' + disty + 'px'
         e.preventDefault()

         var elementTouching = document.elementFromPoint(
         distx,
         disty
         );
         console.log(elementTouching.id);

         var zxc = $(elementTouching.id).css({"background-color":"blue"});
         console.log(zxc);
         }, false)

         demo.addEventListener('touchend', function(e){
         var touchobj = e.changedTouches[0] // reference first touch point for this event
         statusdiv.innerHTML = 'Status: touchend
         <br>
         Resting x coordinate: ' + touchobj.clientX + 'px'
         statusdiv1.innerHTML = 'Status: touchend
         <br>
         Resting y coordinate: ' + touchobj.clientY + 'px'
         e.preventDefault()
         }, false)

         }, false)

Answer

Arun P Johny picture Arun P Johny · Jun 29, 2015

Since you are passing id to jQuery, you need to use id selector

$('#'+elementTouching.id).css({"background-color":"blue"});

or just pass the element itself

$(elementTouching).css({"background-color":"blue"});