1

I want to add a small function to move an element up/down/left/right.

How I can check in which direction an element (div span etc) can move at all?

For example, I have this code:

<div id="stop_here_container">
<div></div>
<div>
  <div><div>
  <div></div>
  <div></div>
</div>
<div></div>
</div>

Now I must check in which direction every element could be moved (elements could be unlimited nested!). For example, to know this:

<div id="stop_here_container">
<div>I can move down</div>
<div>
  <div>I can move up/down/right</div>
  <div>I can move left/right/up/down</div>
  <div>I can move up/down/left</div>
</div>
<div>I can move up</div>
</div>

After that, I can add a data.attr or a class to every element, and go on with his.

Thanks for your help and suggestions

Bilaal Rashid
  • 828
  • 2
  • 13
  • 21
MikeR
  • 83
  • 8

1 Answers1

2

You can use data attribute to store your state and using class you can easily find that state like below.

$(".move").mousedown(function() {
  console.log($(this).data('myval'));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='move' data-myval='down'>i can move down</div>
<div>
  <div class='move' data-myval='up/down/right'> i can move up/down/right</div>
  <div class='move' data-myval='left/right/up/down'> i can move left/right/up/down</div>
  <div class='move' data-myval='up/down/left'> i can move up/down/left</div>
</div>
<div class='move' data-myval='up'>i can move up</div>
4b0
  • 21,981
  • 30
  • 95
  • 142
  • i 'm sorry but i'm afraid to think you missunderstoud me. At the beginning i dont know in wich direction a element could be moved. i've only add the "i can move..." to make clear what i want. I must check every element in wich direction it can move. If i find a way to do that, i cann give them a class or data-attr. like in your suggestion. Did you have a idear how we can do that? – MikeR Feb 02 '19 at 09:21
  • see this old question if it help. https://stackoverflow.com/questions/3861053/how-to-get-the-direction-of-moving-object-in-draggable-plugin-jquery – 4b0 Feb 02 '19 at 09:26
  • i not use draggable, but thanks. i need i for a own scripting so i have to find a way to check the possibilities of moving direction – MikeR Feb 02 '19 at 09:28