Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Please write a simple javascript code for me

    Hello
    Please write the javascript code for me :S
    want to have 37 buttons numbered and ordered from 0 to 36 (0;1;2;3;4;5;...33;34;35;36)
    If I click one of them it must become at the first position of the list.
    for example at first list is ordered from 0 to 36: 0;1;2;3;4;5;...33;34;35;36
    then, after I click button 4, it becomes the first at list: 4;0;1;2;3;5;...33;34;35;36
    then, after I click button 34, it becomes the first at list: 34;4;0;1;2;3;5;...33;35;36
    after this If I click button 35, the list will become: 35;34;4;0;1;2;3;5;...33;36
    and so on..
    Please write it for me :S

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,732
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Homework? This forum is not a free coding service. As a general rule, the people helping out in this forum don't write code for others but try to help with fixing code that doesn't work. You may perhaps get someone to write this script for you, but you'll be far more likely to get help if you have made a substantial effort and written some code yourself. Then someone here will almost certainly help you correct/improve your work.

    You need to have a look at the splice() and unshift() methods of Javascript arrays. The splice() rmethod removes a value from an array, the unshift() method adds a new item to the beginning of an array.

    You would do better if you explained why you wanted this.


    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 06-18-2012 at 03:39 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy

    I don't javascript so I can't write any code, that's why I asked here and hoped that somebody would help me.
    If you can write this script, please write it to me. you will help me a lot this way.

  • #4
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #5
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,467
    Thanks
    3
    Thanked 495 Times in 482 Posts
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    </head>
    <body>
    <input class="button" type="button" name="" value="Button 1" />
    <input class="button" type="button" name="" value="Button 2" />
    <input class="button" type="button" name="" value="Button 3" />
    <input class="button" type="button" name="" value="Button 4" />
    <br />
    <input class="button" type="button" name="" value="Button 5" />
    <input class="button" type="button" name="" value="Button 6" />
    <input class="button" type="button" name="" value="Button 7" />
    <input class="button" type="button" name="" value="Button 8" />
    <br />
    <input class="button" type="button" name="" value="Button 9" />
    <input class="button" type="button" name="" value="Button 10" />
    <input class="button" type="button" name="" value="Button 11" />
    <input class="button" type="button" name="" value="Button 12" />
    <input class="button" type="button" name="" value="Button 13" />
    <input class="button" type="button" name="" value="Button 14" />
    <input class="button" type="button" name="" value="Button 15" />
    <br />
    <input class="button" type="button" name="" value="Button 16" />
    <input class="button" type="button" name="" value="Button 17" />
    <input class="button" type="button" name="" value="Button 18" />
    <input class="button" type="button" name="" value="Button 19" />
    <br />
    <input class="button" type="button" name="" value="Button 20" />
    <input class="button" type="button" name="" value="Button 21" />
    <input class="button" type="button" name="" value="Button 22" />
    <input class="button" type="button" name="" value="Button 23" />
    <input class="button" type="button" name="" value="Button 24" />
    <input class="button" type="button" name="" value="Button 25" />
    <br />
    <input class="button" type="button" name="" value="Button 26" />
    <input class="button" type="button" name="" value="Button 27" />
    <input class="button" type="button" name="" value="Button 28" />
    <input class="button" type="button" name="" value="Button 29" />
    <br />
    <input class="button" type="button" name="" value="Button 30" />
    <input class="button" type="button" name="" value="Button 31" />
    <input class="button" type="button" name="" value="Button 32" />
    <input class="button" type="button" name="" value="Button 33" />
    <input class="button" type="button" name="" value="Button 34" />
    <input class="button" type="button" name="" value="Button 35" />
    <br />
    <input class="button" type="button" name="" value="Button 36" />
    <input class="button" type="button" name="" value="Button 37" />
    
    
    <script  type="text/javascript">
    /*<![CDATA[*/
    
    var zxcSwapButton={
    
     init:function(cls){
      var ary=this.bycls(cls,document),a=[],o={},z0=0;
      o.ary=ary;
      o.lks=[];
      for (var z0=0;z0<ary.length;z0++){
       a=document.createElement('A');
       o.lks[z0]=a;
       ary[z0].parentNode.insertBefore(a,ary[z0]);
       this.addevt(ary[z0],'mouseup','insert',o,ary[z0]);
      }
     },
    
     insert:function(o,obj){
      for (var mve,z0=0,z1=0;z0<o.ary.length;z0++){
       if (o.ary[z0]==obj){
        o.ary.splice(z0,1);
        break;
       }
      }
      o.ary=[obj].concat(o.ary);
      for (;z1<o.lks.length;z1++){
       o.lks[z1].parentNode.insertBefore(o.ary[z1],o.lks[z1]);
      }
     },
    
     addevt:function(o,t,f,p,p1){
      var oop=this;
      if (o.addEventListener) o.addEventListener(t,function(e){ return oop[f](p,p1);}, false);
      else if (o.attachEvent) o.attachEvent('on'+t,function(e){ return oop[f](p,p1); });
     },
    
     bycls:function (nme,el){
      for (var reg=new RegExp('\\W'+nme+'\\W'),els=el.getElementsByTagName('*'),ary=[],z0=0; z0<els.length;z0++){
       if(reg.test(' '+els[z0].className+' ')){
        ary.push(els[z0]);
       }
      }
      return ary;
     }
    
    }
    
    zxcSwapButton.init('button');
    /*]]>*/
    </script>
    </body>
    
    </html>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,732
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by laperashvili View Post
    I don't javascript so I can't write any code, that's why I asked here and hoped that somebody would help me.
    If you can write this script, please write it to me. you will help me a lot this way.
    A special favour, and because I am in a good mood today, I am giving you this code, but please do not ask for people to write your Javascript code again. You should pay for a professional as in any other field in life. I take it you can extend the number of buttons from 6 to 37 by yourself.

    Code:
    <html>  
    <head> 
    <body>
    
    <input type = "button" value = "Button 1" onclick = "makeArray(1)">
    <input type = "button" value = "Button 2" onclick = "makeArray(2)">
    <input type = "button" value = "Button 3" onclick = "makeArray(3)">
    <input type = "button" value = "Button 4" onclick = "makeArray(4)">
    <input type = "button" value = "Button 5" onclick = "makeArray(5)">
    <input type = "button" value = "Button 6" onclick = "makeArray(6)">
    
    <script type = "text/javascript">
    
    var myArray = [1,2,3,4,5,6];
    
    function makeArray(which) {
    for (var i =0; i<myArray.length; i++) {
    if (myArray[i] == which) {
    myArray.splice(i,1);  // remove the element
    }
    }
    
    myArray.unshift(which);  // place the chosen value at start of the array
    alert (myArray);
    
    }
    
    </script>
    
    </body>
    </html>
    Loking at Vic's code it looks as though I did not interpet your requirement correctly. I though you wanted a list (array) of numbers 1-37 with the buttons changing the order of that array.
    Last edited by Philip M; 06-18-2012 at 03:58 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by laperashvili View Post
    Hello
    Please write the javascript code for me
    Please write it for me :S
    What is the purpose of this code?

    Where will it be used?

    What is your teacher's email address?

  • #8
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,772
    Thanks
    55
    Thanked 518 Times in 515 Posts
    Code:
    <script type="text/javascript" >
    for(var i=0; i<37; i++ ){
    butt=document.createElement("button");
    butt.appendChild(document.createTextNode(i));
    butt.onclick=function(){
    document.body.insertBefore(this,document.body.firstChild)
    	}
    document.body.appendChild(butt);
    }
    </script>

  • #9
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,772
    Thanks
    55
    Thanked 518 Times in 515 Posts
    Quote Originally Posted by iBall View Post
    According to w3schools (firstChild) -
    so the wrong first child might be picked in some cases.
    interesting, but hardly relevant, unless you're going out of your way to make the code fail.

    FYI, this line is redundant:
    Code:
    btnsContainerO.removeChild(elem);
    as insertBefore removes the element from the DOM before repositioning it

  • #10
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,772
    Thanks
    55
    Thanked 518 Times in 515 Posts
    "break"? no, that will never happen.

    apart from the fact that your first and last sentences say basically the same thing, what I posted was for demo purposes only (sound familiar?) - obviously there are better solutions than appending straight to the doc.body, and obviously if the op starts altering the markup later and things start going pear shaped they will know why, but thanks for pointing that out anyhow.

  • #11
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,471
    Thanks
    0
    Thanked 634 Times in 624 Posts
    Run the following function first (on the body of the page) and the extra empty text nodes will be removed (as well as any text nodes you split into two will be merged back into one so that there will only ever be one text node between tags).

    Code:
    normalizeAll = function(node) {
      "use strict";
      var i, c; 
      for (i=node.childNodes.length-1; i >= 0; i--) {
        c = node.childNodes[i];
        if((3 === c.nodeType && !/\S/.test(c.nodeValue))) || 8 === c.nodeType) {
          node.removeChild(child);
        }
        if(1 === c.nodeType) {
          normalizeAll(c);
        }
      }
      node.normalize(); 
      return node;
    }
    Note that this deliberately loops backward so as to not need to reposition itself when deleting a node - everything after the deleted node having already been processed. It also avoids having to recheck the length each time around the loop (since it changes whenever you delete) because the number of nodes from where you are to the start is unchanged.
    Last edited by felgall; 06-19-2012 at 09:55 AM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #12
    New Coder
    Join Date
    Jun 2011
    Location
    Australia
    Posts
    13
    Thanks
    3
    Thanked 1 Time in 1 Post
    Quote Originally Posted by iBall View Post
    just wanted to make sure the op was aware of the potential pitfalls of using firstChild as described in the w3schools link I posted, and how to avoid them.
    maybe op would think firstChild is his older brother?


  •  

    Posting Permissions

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