...

View Full Version : Please write a simple javascript code for me



laperashvili
06-18-2012, 03:19 PM
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

Philip M
06-18-2012, 04:02 PM
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.

laperashvili
06-18-2012, 04:15 PM
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. :(

WolfShade
06-18-2012, 04:20 PM
http://www.freelancer.com/

vwphillips
06-18-2012, 04:45 PM
<!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>

Philip M
06-18-2012, 04:48 PM
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.


<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.

Logic Ali
06-18-2012, 07:48 PM
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?

xelawho
06-19-2012, 03:40 AM
<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>

xelawho
06-19-2012, 04:36 AM
According to w3schools (firstChild) (http://www.w3schools.com/dom/prop_element_firstchild.asp) -
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:

btnsContainerO.removeChild(elem);

as insertBefore removes the element from the DOM before repositioning it

xelawho
06-19-2012, 05:23 AM
"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.

felgall
06-19-2012, 10:52 AM
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).


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.

Scyko
06-19-2012, 01:42 PM
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?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum