anseki / plain-draggable

The simple and high performance library to allow HTML/SVG element to be dragged.
https://anseki.github.io/plain-draggable/
MIT License
765 stars 96 forks source link

How to use getElementById #11

Closed mekkolodziejski closed 5 years ago

mekkolodziejski commented 5 years ago

Hello There is a possibility to add array items or dynamically (multiple) to snap targets?

anseki commented 5 years ago

Hi @szybki8562, thank you for the comment. Yes of course you can change options including the snap dynamically.

mekkolodziejski commented 5 years ago

can u help me how ? im new in js THX

anseki commented 5 years ago

For example: https://jsfiddle.net/yjmhdr2a/

mekkolodziejski commented 5 years ago

thanks for the answer, but it was not what I was talking about

I have random id elements added while the application is running. I asked if it can be added to the current snap or from whole array svg

I will show you a piece of code that I tried. maybe it will be easier for you to understand me

for (var it in ARRAY) { TEMPARRAY.push('document.getElementById("' + ARRAY[it].id + '")'); } draggable.snap = { targets: TEMPARRAY, gravity: 20, edge: 'outside', };

or +=

anseki commented 5 years ago

It seems that the example is what you want. You can specify snap targets even if those have random IDs. See line 4.

mekkolodziejski commented 5 years ago

i cant write this way

draggable.snap = [ **_document.getElementById('box-a'), document.getElementById('box-b')_** ];

i dont have this elements at start and i dont know what will the id elements that I want to attract, adds to the array with time

if he puts this code in the loop, the appeal is only in the last element

anseki commented 5 years ago

I merged the example and your code: https://jsfiddle.net/yjmhdr2a/1/ BTW, the TEMPARRAY and gravity option are not needed.

anseki commented 5 years ago

Optimized: https://jsfiddle.net/yjmhdr2a/2/

mekkolodziejski commented 5 years ago

`ARRAY = [{id: 'box-a'}, {id: 'box-b'}];

var TEMPARRAY = ARRAY.map(function(target) { return document.getElementById(target.id);});

alert(TEMPARRAY);`

in my proj alert is null in jsfiddle OK

anseki commented 5 years ago

Sorry, my English is poor. What do you mean?

mekkolodziejski commented 5 years ago

TEMPARRAY have empty

anseki commented 5 years ago

Did you try this? https://jsfiddle.net/yjmhdr2a/2/

mekkolodziejski commented 5 years ago

ARRAY.map propably this not working

mekkolodziejski commented 5 years ago

something is wrong with my project your fiddle is OK

THX for yr time

anseki commented 5 years ago

The null is different from "empty". You should clear up that. If that is "empty", the getElementById couldn't get elements. If that is null, your code has problem.

mekkolodziejski commented 5 years ago

i have comma

in jsfiddle

[object HTMLSpanElement],[object HTMLSpanElement]

same code add at start

` ARRAY = [{ id: 'box-a' }, { id: 'box-b' }];

for (var j in ARRAY) {
    alert(ARRAY[j].id); <<<<----->>>> HERE IS OK I HAVE VALUES
}

var a = ARRAY.map(function (target) { return document.getElementById(target.id) });

alert(a); <<<<----->>>> HERE IS ONLY COMMA

`

anseki commented 5 years ago

For example:

var ARRAY = [{id: 'box-a'}, {id: 'box-b'}],
  TEMPARRAY = ARRAY.map(function(target) { return document.getElementById(target.id); });

This code gets elements box-a and box-b. In other words, that gets nothing if there are not box-a and box-b.

anseki commented 5 years ago

What is "comma"?

mekkolodziejski commented 5 years ago

,

anseki commented 5 years ago

I know "comma". What do you mean "i have comma"?

mekkolodziejski commented 5 years ago

array.map return only comma without values in alert before map is ok have values

anseki commented 5 years ago

Please don't change comments that were read because we can't have conversation. So, could you explain the current progress?

mekkolodziejski commented 5 years ago

I have no value after the array map

anseki commented 5 years ago

What do you mean "no value"?

anseki commented 5 years ago

Try this:

console.log(ARRAY);
mekkolodziejski commented 5 years ago

`ARRAY = [{id: 'box-a'}, {id: 'box-b'}];

var a = ARRAY.map(function(target) { return document.getElementById(target.id)});

alert(a); <<<--->>> jsfiddle = [object HTMLSpanElement],[object HTMLSpanElement] my proj = , ` console same values

debugger no error

anseki commented 5 years ago

In console, try this:

document.getElementById('box-a')
mekkolodziejski commented 5 years ago

array map needs some library?

anseki commented 5 years ago

What is your environment? browser and version?

mekkolodziejski commented 5 years ago

vs code jquery 3.3.1 opera

anseki commented 5 years ago

Which version of Opera?

mekkolodziejski commented 5 years ago

pure html with Githubissues.

  • Githubissues is a development platform for aggregating issues.