Javascript Course

Move image from an element to another - with jQuery

The JavaScript script from this page can be used to move /add an image from an element to another.
It is created in two versions, with standard JavaScript, and with jQuery effects.

• To Download this script, with the examples presented below, click: Move image from an element /tag to another.


- Usage:
1. Copy the code of the script (displayed below) into your HTML document, at the end, before the closing tag </body>
2. In the obimids variable add the path of the image associated with the IDs of the two elements in which the image will alternate when click on it. You can add how many images you want, using this syntax:
'image_path': ['id_elm1', 'id_elm2']
  - "id_elm1" is the ID of the tag where the image is initially displayed.
  - "id_elm2" is the ID of the second tag, where the image is moved, when click on the first tag.
• For example, adding 2 images:
var obimids = {
  'dirimg/image.jpg': ['idfrom', 'idto'],
  'dirimg/image2.jpg': ['idfrom2', 'idto2']
};
3. In your HTML document create the tags with the IDs that was associated to the image, in which the image will be displayed and moved.
• For example, if you add in obimids 2 images (like in the example above), in the HTML document add 4 elements (you can use CSS properties to position and design those elements):
<div id="idfrom"></div>
<div id="idfrom2"></div>
<div id="idto"></div>
<div id="idto2"></div>

- Code of the script:
<script type="text/javascript">
/*
 Here add:
    'image_path': ['id_elm1', 'id_elm2']
 "id_elm1" is the ID of the tag where the image is initially displayed
 "id_elm2" is the ID of the second tag, where the image is moved, when click on the first tag
*/
var obimids = {
  'imgage1.jpg': ['ide1', 'ideto1'],
  'image2.jpg': ['ide2', 'ideto2']
};

// function executed when click to move the image into the other tag
function whenAddImg() {
  /* Here you can add a code to be executed when the images is added in the other tag */
  return true;
}

         /* From here no need to edit */

// create object that will contain functions to alternate image from a tag to another
var obaImg = new Object();
 // coursesweb.net/javascript/
  // put the image in element with ID from "ide"
  obaImg.putImg = function(img, ide) {
    if(document.getElementById(ide)) {
      document.getElementById(ide).innerHTML = '<img src="'+ img+ '" />';
    }
  }

  // empty the element with ID from "elmid", add image in the other element associated to "img"
  obaImg.alternateImg = function(elmid) {
    var img = obaImg.storeim[elmid];
    var addimg = (elmid == obimids[img][0]) ? obimids[img][1] : obimids[img][0];
    document.getElementById(elmid).innerHTML = '';
    obaImg.putImg(img, addimg);

    // function executed after the image is moved into "addimg"
    whenAddImg();
  }
  obaImg.storeim = {};            // store /associate id_elm: image

  // add 'image': 'id_elm1', and 'image': 'id_elm1' in "storeim"
  // add the image in the first tag associated to image
  // register 'onclick' to each element associated with images in "obimids"
  obaImg.regOnclick = function() {
    for(var im in obimids) {
      obaImg.storeim[obimids[im][0]] = im;
      obaImg.storeim[obimids[im][1]] = im;
      obaImg.putImg(im, obimids[im][0]);
      document.getElementById(obimids[im][0]).onclick = function(){ obaImg.alternateImg(this.id); };
      document.getElementById(obimids[im][1]).onclick = function(){ obaImg.alternateImg(this.id); };
    }
  }
obaImg.regOnclick();       // to execute regOnclick()
</script>

• The script contains a function ( whenAddImg() ) that is called after the image is moved into the other tag. You can use this function to perform some instructions when the image is moved from one tag into another.
- See also the comments in code.

Example: (Click on image to move it in other element, then click again on image to move it back to the initial place):
Move image into other HTML tag.

Move image from an element to another - with jQuery

Here is the same script, but with jQuery effects. The images are added the same as explained above, in the obimids object (see also the comments in code).

- Code of the script - with jQuery:
<script type="text/javascript">
/*
 Here add:
    'image_path': ['id_elm1', 'id_elm2']
 "id_elm1" is the ID of the tag where the image is initially displayed
 "id_elm2" is the ID of the second tag, where the image is moved, when click on the first tag
*/
var obimids = {
  'imgage1.jpg': ['ide1', 'ideto1'],
  'image2.jpg': ['ide2', 'ideto2']
};

// function executed when click to move the image into the other tag
function whenAddImg() {
  /* Here you can add a code to be executed when the images is added in the other tag */
  return true;
}

         /* From here no need to edit */

// create object that will contain functions to alternate image from a tag to another
var obaImg = new Object();
 // coursesweb.net/javascript/
  // put the image in element with ID from "ide"
  obaImg.putImg = function(img, ide, stl) {
    if(document.getElementById(ide)) {
      document.getElementById(ide).innerHTML = '<img src="'+ img+ '" '+stl+' />';
    }
  }

  // empty the element with ID from "elmid", add image in the other element associated to "img"
  obaImg.alternateImg = function(elmid) {
    var img = obaImg.storeim[elmid];
    var addimg = (elmid == obimids[img][0]) ? obimids[img][1] : obimids[img][0];
    $('#'+elmid+ ' img').hide(800, function(){
      $('#'+elmid).html('');
      obaImg.putImg(img, addimg, 'style="display:none;"');
      $('#'+addimg+ ' img').fadeIn(500);
    });

    // function executed after the image is moved into "addimg"
    whenAddImg();
  }
  obaImg.storeim = {};            // store /associate id_elm: image

  // add 'image': 'id_elm1', and 'image': 'id_elm1' in "storeim"
  // add the image in the first tag associated to image
  // register 'onclick' to each element associated with images in "obimids"
  obaImg.regOnclick = function() {
    for(var im in obimids) {
      obaImg.storeim[obimids[im][0]] = im;
      obaImg.storeim[obimids[im][1]] = im;
      obaImg.putImg(im, obimids[im][0], '');
      document.getElementById(obimids[im][0]).onclick = function(){ obaImg.alternateImg(this.id); };
      document.getElementById(obimids[im][1]).onclick = function(){ obaImg.alternateImg(this.id); };
    }
  }
obaImg.regOnclick();       // to execute regOnclick()
</script>

• The script contains a function ( whenAddImg() ) that is called after the image is moved into the other tag. You can use this function to perform some instructions when the image is moved from one tag into another.

Example: (Click on image to add it into the picture below, then click again on image to move it back to the initial place):
Add image from a tag to other element, and back.

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Move image from an element /tag to another

Last accessed pages

  1. sPBM - Simple PHP Backup Manager (3402)
  2. Vue JS - Transition and Animation (490)
  3. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (141749)
  4. Node.js Move and Copy file (28420)
  5. MouseEvent - Events for Mouse (2909)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (473)
  2. CSS cursor property - Custom Cursors (79)
  3. The Mastery of Love (70)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (62)
  5. CSS3 2D transforms (46)