/    Sign up×
Community /Pin to ProfileBookmark

Newbie help please…..

I am trying do do what I thought was simple…I have a middle ware app making a list of form fields. The field elements are text and contain numbers only. The numbers in the text fields (what the user enters) are used to detemine the “order of a list”. So….If there are 5 form fields on a page, then the text fields would be numbered 1,2,3,4,5. The user has the option of “changing the order” of the products listed, so…I have figured out how to limit the entry to numbers only, along with not allowing the entry of a number larger than the number of text fields. (if there are only 5 fields, then the “max order number” can only be 5, so…
Here is my problem…when a user changes one of the text fields, we ONE other text field on the form will now contain a duplicate value, so we need to “swap” the values. In other words, no text fields can contain the same “order by number”. I have tried to loop through the form elements to compare, then switch but it doesn’t seem to work?? Here is my code….

formField.value is “passed in to the function”

var y = formField.value
var z = 0
for (x=0; x < document.forms[0].elements.length; x++){
//get the value of the element being inspected
z = document.forms[0].elements[x].value;
//if z is == y then switch the values
if(z >= y){
document.forms[<%=iWhatForm%>].elements[x].value = y;
formField.value = z;
Thanks in advance…
David

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@NedalsFeb 27.2003 — Interesting!!!

<html><head><title>Untitled</title>

<script type="text/javascript">

var numAry = new Array('','','','','',''); // used to track values prior to change for each cell

function update(num,thiscell) {

&nbsp;&nbsp;var imax = 5; // maximium value allowable

&nbsp;&nbsp;var anothercell = -1; // set to -1 since valid num between 0 and (imax-1)

&nbsp;&nbsp;if ((num<0) || (num>imax)) { alert(num + ' is out of range'); return; }

&nbsp;&nbsp;// loop to check if num already exists in ANOTHER cell and get the cell number

&nbsp;&nbsp;for (var i=0; i<imax; i++) {

&nbsp;&nbsp;&nbsp;&nbsp;if ((num == document.theform.elements[i].value) && (thiscell != i)) { anothercell = i }

&nbsp;&nbsp;}

&nbsp;&nbsp;if (anothercell > -1) { // another value found

&nbsp;&nbsp;&nbsp;&nbsp;numAry[anothercell] = document.theform.elements[thiscell].value;

&nbsp;&nbsp;&nbsp;&nbsp;document.theform.elements[anothercell].value = numAry[thiscell]; // update the array and the anothercell.value

&nbsp;&nbsp;}

&nbsp;&nbsp;numAry[thiscell] = num;

&nbsp;&nbsp;document.theform.elements[thiscell].value = num; // update array and this cell

}

</script>

</head>

<body bgcolor="#ffffff">

<form name="theform">

<input type="text" onChange="update(this.value,0)"><br>

<input type="text" onChange="update(this.value,1)"><br>

<input type="text" onChange="update(this.value,2)"><br>

<input type="text" onChange="update(this.value,3)"><br>

<input type="text" onChange="update(this.value,4)"><br>

</form>

</body></html>



Try this! ?
Copy linkTweet thisAlerts:
@davidbookauthorFeb 27.2003 — Look like it will work, (i'm laughing, like I would know), I'll get figured out then post back.
Copy linkTweet thisAlerts:
@davidbookauthorFeb 27.2003 — //here is what i have

<script Language = Javascript>

//this script is designed to find duplicate form field entries,

//then "swap" the values for the order by list

// <% %> contained in the javascript are server side to build

//dynamic array of elements to "plant" in our client side javascript, so....

function update(num,thiscell){

var numAry = new <%=arrThis%>; // used to track values

//prior to change for each cell , arrThis will

// look like Array('','','','') etc.

var imax = <%=iHowMany%>;

// maximium value allowable

var anothercell = -1;

// set to -1 since valid num between 0 and (imax-1)

if ((num<0) || (num>imax)) {

alert(num + ' is out of range');

return;}

// loop to check if num already exists in ANOTHER cell and

//get the cell number

for (var i=0; i<imax; i++){

if ((num == document.<%=iWhatForm%>.elements[i].value) && (thiscell != i)){

anothercell = i

}

}

if (anothercell > -1){ // another value found , there will always will be a matching number when a form field is changed

numAry[anothercell] = document.<%=iWhatForm%>.elements[thiscell].value;

document.<%=iWhatForm%>.elements[anothercell].value = numAry[thiscell]; // update the array and the anothercell.value

}

numAry[thiscell] = num;

document.<%=iWhatForm%>.elements[thiscell].value = num;

// update array and this cell

}

</script>



I am getting an "object Expeted" error in browser status bar...I looked at the source after running the script and all looks exactly like i expected. The <% %> inserted just what I wanted. ....Any ideas?
Copy linkTweet thisAlerts:
@NedalsFeb 27.2003 — 
  • 1. The numAry must be outside the function. If you are not predefining the numbers, it can be preset as shown.


  • 2. I recomend you add (inside the function)

    var thisform = document.<%=iWhatForm%>


  • and replace (4 places I think)

    ' document.<%=iWhatForm%>. ' with ' thisform. '

    if (anothercell > -1){ // another value found , there will always will be a matching number when a form field is changed[/quote]
    Only if you set the value in each cell server side, otherwise they will be null. The values in the cells and the array MUST match at startup.

    I'm not familiar with SSI so I can't help in that area.
    Copy linkTweet thisAlerts:
    @davidbookauthorFeb 28.2003 — It should work with your input. I already did the myForm = variable thing....thanks
    ×

    Success!

    Help @davidbook spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.10,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...