Jump to content

dynamic text boxes: changing the rank


jdubwelch

Recommended Posts

I have any number of text boxes, lets say 4 for example.  Named: a, b, c, & d.  Each start with the values 1,2,3, & 4.  I want to make it so when the user changes the rank, it will swap with the other box that had that value. 

 

For example, lets say the user changes box "c" from 3 to 1.  I then want box "a" to automatically change to 3. 

 

Also, I want them to do it as much as they want and still work. 

 

I'm pretty new at javascript, I know php pretty well and would use an array in php but arrays in javascript are quite the same.

 

This is what i have so far, but I don't know if it's in the right direction.  Any help would be great, or any articles or tutorials you know of would be awesome too.  thanks.

 

<script type="text/javascript">
function updateForm (newValue) {
     var newVal = newValue.value;
     alert("the value of the box that was changed is: " + newVal);
}

function currentVal (currentValue) {
var currentVal = currentValue.value;
var currentName = currentValue.name;
alert("the current value of the box is: " + currentName);
}
</script>
<form id="rankem" name="rankem" method="post" action="">
  <p>
    <input name="a" type="text" id="a" size="3"  value="1" onchange="updateForm(this);" />
  </p>
  <p>
    <input name="b" type="text" id="b" size="3" value="2" onchange="updateForm(this);" />
  </p>
  <p>
    <input name="c" type="text" id="c" size="3"  value="3" onchange="updateForm(this);" />
  </p>
  <p>
    <input name="d" type="text" id="d" size="3" value="4" onchange="updateForm(this);" />
  </p>
</form>

 

i need the current value, and the new value... i can get them with my different functions... but i don't know how to get them together at once.

Link to comment
Share on other sites

If you don't need id values.

Or you can use style.zIndex as a temporary storage.

<script type="text/javascript">
function SwapValues(frst, sec) {
  var val = frst.value;
  frst.value = sec.id;
  sec.value = val;
  delete frst; delete sec; delete val;
}
function updateForm (which) {
  if (document.rankem.a != which && document.rankem.a.value == which.value) SwapValues(document.rankem.a, which);
  else
  if (document.rankem.b != which && document.rankem.b.value == which.value) SwapValues(document.rankem.b, which);
  else
  if (document.rankem.c != which && document.rankem.c.value == which.value) SwapValues(document.rankem.c, which);
  else
  if (document.rankem.d != which && document.rankem.d.value == which.value) SwapValues(document.rankem.d, which);

  delete which;
}
</script>
<form id="rankem" name="rankem" method="post" action="">
  <p>
    <input name="a" type="text" id="0" size="3" value="1" onfocus="this.id=this.value" onchange="updateForm(this);" />
  </p>
  <p>
    <input name="b" type="text" id="0" size="3" value="2" onfocus="this.id=this.value" onchange="updateForm(this);" />
  </p>
  <p>
    <input name="c" type="text" id="0" size="3" value="3" onfocus="this.id=this.value" onchange="updateForm(this);" />
  </p>
  <p>
    <input name="d" type="text" id="0" size="3" value="4" onfocus="this.id=this.value" onchange="updateForm(this);" />
  </p>
</form>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.