www.webdeveloper.com
Results 1 to 8 of 8

Thread: Moving Image to First

Hybrid View

  1. #1
    Join Date
    Feb 2014
    Posts
    12

    Moving Image to First

    Hello,
    I have 6 buttons, one for each operation. I am able to hide all images, show all images, display even images, and display odd images.

    I am failing to move the first image to last and the last image to first.

    Please assist.

    JavaScript:

    $(function(){
    $("#HIDE").click(function (){$("img").hide();});
    $("#SHOW").click(function (){$("img").show();});
    $("#EVEN").click(function (){$("img: odd").hide();
    $("img:even").show();});
    $("#ODD").click(function (){$("img:even").hide();
    $("img: odd").show();});



    $("#FIRST_is_NOW_last").click(function
    (){$("img:first").parent().append($("img:last"));});
    $("#LAST_is_NOW_first").click(function
    (){$("img:last").parent().prepend($("img:first"));});
    });

    My HTML:

    .
    .
    .
    .
    .
    <div id="myMaterial">
    <img src="pic1.jpg" alt="pic1" id="pic1" title="pic1" />
    <img src="pic2.jpg" alt="pic2" id="pic2" title="pic2" />
    <img src="pic3.jpg" alt="pic3" id="pic3" title="pic3" />
    <img src="pic4.jpg" alt="pic4" id="pic4" title="pic4" />


    </div>
    <div id="output"></div>
    </body>

    </html>

  2. #2
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,136
    can you attach the images that you are using and a sample page with full code(that you say is working partially)...I will take a look
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  3. #3
    Join Date
    Feb 2014
    Posts
    12
    Thanks for your response. May I send you an email?

  4. #4
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,136
    sure...send me a pm
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  5. #5
    Join Date
    Feb 2014
    Posts
    12
    Still trying to figure out how to send a PM I'm new to this forum.

  6. #6
    Join Date
    Feb 2014
    Posts
    12
    I think my issue is in the JS and that's all the code for it that I have posted. My HTML doesn't have much to it.

  7. #7
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,434

    Lightbulb

    Don't see why you cannot supply the images, but make your substitutions as necessary.
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    
    <title> Untitled </title>
    <style type="text/css">
    img { margin:5px; }
    </style>
    
    </head>
    <body>
    <div id="myMaterial"></div>
    <button onclick="imgRotate()">Rotate</button>
    
    <script type="text/javascript">
    var baseURL = 'http://www.nova.edu/hpd/otm/pics/4fun/';
    var imgList = ['11.jpg','22.jpg','33.jpg','44.jpg','55.jpg'];
    
    window.onload = function() {
      var str = '', altInfo = 'pic', titleInfo = 'pic';
      for (var i=0; i<imgList.length; i++) {
        str += '<img src="'+baseURL+imgList[i]+'" alt="'+altInfo+i+'" title="'+titleInfo+i+'" />';
      } document.getElementById('myMaterial').innerHTML = str;
    }
    
    function imgRotate() {
      var sel = document.getElementsByTagName('img');
      var pic = sel[0].src;
      for (var i=1; i<imgList.length; i++) {
        sel[i-1].src = sel[i].src;  // could also move the 'alt' and 'title' value if necessary
      }
      sel[imgList.length-1].src = pic;
    }
    </script>
    
    </body>
    </body>
    </html>

  8. #8
    Join Date
    Feb 2014
    Posts
    12
    Thank you all. I got it to work using the simple .before and .insertAfter

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



Recent Articles