Results 1 to 3 of 3

Thread: Drag and drop + DHTML

  1. #1
    Join Date
    May 2011

    Drag and drop + DHTML


    I am currently learning JavaScript and DHTML and I am trying to figure out how to create drag and drop elements. The code I have written is as follows:

    <title>Drag and Drop test</title>
    <script language="JavaScript">
    var object=null;
    var dX=0;
    var dY=0;

    function initPage(){


    function pickIt(e){
    if (!e){
    var objectID=(e.target)?e.target.id: ((e.srcElement)?e.srcElement.id:null);
    var object=document.getElementById(objectID);

    function dragIt(e){
    if (!e){

    if (object){

    return false;

    function dropIt(e){
    if (!e){
    if (object){
    return false;
    <style type="text/css">

    <body onLoad="initPage()">
    <span class="feature"><img id="body"style="position:absolute;top:123px;left:400px;zIndex=0;"src="BODY.GIF"alt="body"height="326 "width="225"/></span>

    the pickIt(e) function, gets the object location on the screen, however this is where everything stops! With the dragIt () function I was hoping to move the object which is has been selected in pickIt, and which now should have a zIndex of 100, and then drop it at a new location with the dropIt function.

    Any clues on where I have gone wrong.


  2. #2
    Join Date
    May 2011
    Figured it out! I had made an error in the pickIt() function. It should read:
    function pickIt(e){
    if (!e){
    var objectID=(e.target)?e.target.id(e.srcElement)?e.srcElement.id:null);
    if (objectID) object=document.getElementById(objectID);


    In the original verison the I had not give a value to the "object" variable before putting it into the function.

  3. #3
    Join Date
    Oct 2012

    Drag Drop with JavaScript in HTML

    I am also teaching myself JS and HTML with a view to creating teaching materials on an intranet basis. I am a qualified Teacher of English as a Foreign Language (TEFL).
    I have constructed a simple board game to teach reasoning.

    <style type="text/css">

    body { background-color: #AFEEEE; }

    div#board { left: 300px; top: 200px; position: absolute; Width: 494px; height: 494px;
    border: 3px solid black; color: black; background-color: #FFDEAD; z-index: 0;}
    div#sq1, div#sq2,div#sq3, div#sq4
    { width: 60px; height:60px; background-color: yellow; border: 2px solid black;
    top: -2px; position: absolute; z-index: 1;}
    div#sq1 { left: -2px;} div#sq2 {left: 122px;} div#sq3 {left: 246px;} div#sq4 {left: 370px;}
    div#sq5, div#sq6,div#sq7, div#sq8
    { width: 60px; height:60px; background-color: yellow; border: 2px solid black; color: black;
    top: 60px; position: absolute; z-index: 1;}
    div#sq5 { left: 60px;} div#sq6 {left: 184px;} div#sq7 {left: 308px;} div#sq8 {left: 432px;}
    div#sq9, div#sq10,div#sq11, div#sq12
    { width: 60px; height:60px; background-color: yellow; border: 2px solid black; color: black;
    top: 122px; position: absolute; z-index: 1;}
    div#sq9 { left: -2px;} div#sq10 {left: 122px;} div#sq11 {left: 246px;} div#sq12 {left: 370px;}
    div#sq13, div#sq14,div#sq15, div#sq16
    { width: 60px; height:60px; background-color: yellow; border: 2px solid black; color: black;
    top: 184px; position: absolute; z-index: 1;}
    div#sq13 { left: 60px;} div#sq14 {left: 184px;} div#sq15 {left: 308px;} div#sq16 {left: 432px;}
    div#sq17, div#sq18, div#sq19, div#sq20
    { width: 60px; height:60px; background-color: yellow; border: 2px solid black; color: black;
    top: 246px; position: absolute; z-index: 1;}
    div#sq17 { left: -2px;} div#sq18 {left: 122px;} div#sq19 {left: 246px;} div#sq20 {left: 370px;}
    div#sq21, div#sq22,div#sq23, div#sq24
    { width: 60px; height:60px; background-color: yellow; border: 2px solid black; color: black;
    top: 308px; position: absolute; z-index: 1;}
    div#sq21 { left: 60px;} div#sq22 {left: 184px;} div#sq23 {left: 308px;} div#sq24 {left: 432px;}
    div#sq25, div#sq26, div#sq27, div#sq28
    { width: 60px; height:60px; background-color: yellow; border: 2px solid black; color: black;
    top: 370px; position: absolute; z-index: 1;}
    div#sq25 { left: -2px;} div#sq26 {left: 122px;} div#sq27 {left: 246px;} div#sq28 {left: 370px;}
    div#sq29, div#sq30, div#sq31, div#sq32
    { width: 60px; height:60px; background-color: yellow; border: 2px solid black; color: black;
    top: 432px; position: absolute; z-index: 1;}
    div#sq29 { left: 60px;} div#sq30 {left: 184px;} div#sq31 {left: 308px;} div#sq32 {left: 432px;}
    div#d1, div#d2, div#d3, div#d4
    { width: 40px; height:40px; background-color: red; border: 2px solid black; color: black;
    top: 8px; left: 8px; position: absolute; z-index: 2;}
    { width: 40px; height:40px; background-color: blue; border: 2px solid black; color: black;
    top: 8px; left: 8px; position: absolute; z-index: 2;}
    <script type="text/javascript">
    function allowDrop(ev) { ev.preventDefault(); }
    function drag(ev) { ev.dataTransfer.setData("Text",ev.target.id); }
    function drop(ev) { var data=ev.dataTransfer.getData("Text");
    ev.preventDefault(); }


    <div id="board">
    <div id="sq1" ondrop="drop(event)" ondragover="allowDrop(event)">
    <div id="d1" draggable="true" ondragstart="drag(event)"></div></div>

    <div id="sq2" ondrop="drop(event)" ondragover="allowDrop(event)">
    <div id="d2" draggable="true" ondragstart="drag(event)"></div></div>

    <div id="sq3" ondrop="drop(event)" ondragover="allowDrop(event)">
    <div id="d3" draggable="true" ondragstart="drag(event)"></div></div>

    <div id="sq4" ondrop="drop(event)" ondragover="allowDrop(event)">
    <div id="d4" draggable="true" ondragstart="drag(event)"></div></div>

    <div id="sq5" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
    <div id="sq6" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
    <div id="sq7" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
    <div id="sq8" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
    <div id="sq9" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
    <div id="sq10" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
    <div id="sq11" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
    <div id="sq12" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
    <div id="sq13" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
    <div id="sq14" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
    <div id="sq15" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
    <div id="sq16" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
    <div id="sq17" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
    <div id="sq18" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
    <div id="sq19" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
    <div id="sq20" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
    <div id="sq21" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
    <div id="sq22" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
    <div id="sq23" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
    <div id="sq24" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
    <div id="sq25" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
    <div id="sq26" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
    <div id="sq27" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
    <div id="sq28" ondrop="drop(event)" ondragover="allowDrop(event)"></div>

    <div id="sq29" ondrop="drop(event)" ondragover="allowDrop(event)">
    <div id="d5" draggable="true" ondragstart="drag(event)"></div></div>

    <div id="sq30" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
    <div id="sq31" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
    <div id="sq32" ondrop="drop(event)" ondragover="allowDrop(event)"></div>


    If you copy this in Notepad and save it as html you will see that it allows each piece to be Dragged and Dropped (DND) onto any of the 32 playing squares.

    The purpose of the game is for blue to get to the top of the board, moving first, then alternately with any red, one square per turn in any diagonal direction. Red tries to prevent this and moves similarly to blue except that only downward diagonal movement is allowed.

    The game suffers from some points:
    1) the moves need to be restricted as per the movement rules.
    2) any piece should not be allowd to occupy any playing square already occupied
    3) a counter is needed to regulate the alternate move sequence
    4) it would be nice to randomize the initial position of blue at the start of the game

    Any ideas on how to do this?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.10575 seconds
  • Memory Usage 2,880KB
  • Queries Executed 15 (?)
More Information
Template Usage (32):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (3)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (3)postbit
  • (3)postbit_onlinestatus
  • (3)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates