Re: Array iteration - set keyup on input - sets all inputs to same value, not dynamic value

Wednesday, February 5, 2014

Mike,

Thanks for your reply!

Believe me, I would LOVE to use jQuery instead of MooTools.  Unfortunately, I am working as part of a team (and I'm the most recently hired), and the team decided to go with MooTools, instead.  So, I am still learning it, myself.  I understand jQuery much better.

I tried creating a separate function for adding the keyup event, and got an error message stating that one of the elements is null.  I placed it inside the domready section, so I don't know why I'm getting that, as the element should exist, at that point.

MooTools also has an "each" function, but I'm getting the same issue, that way - all "in" inputs get the keyup that will apply the value of the last "in" input to all "out" inputs, instead of the associated "in" input.

I also tried putting a conditional in place, but each iteration of the for loop appears to be looping through all the "in" inputs (a loop within a loop!) for each input that I'm trying to give a keyup event to.  I thought that setting the keyup on each would get only the associated "out" instead of all of them.  I'm flaking on a piece of logic, and I'm just not seeing it.

Thank you,

JD


On Wed, Feb 5, 2014 at 12:20 PM, Michael J. Fuhrman <mfuhrman@enetarch.net> wrote:
JD,

While I understand what you are trying to accomplish, I am not familiar with the method you are using (MooTools ). With that said, in reviewing the code, I tried to see if it was filtering INPUT tags with the style class IN or OUT. I do not see such a filter or IF statement.  This might be what you are missing.

Also, I would use JQuery, and the code would look something like:

$("#rates").find("input.out").each( function (this) { this.on ("keyup", onKeyUp); } );

function onKeyUp (thisID, outID)
{ $(outID).set('value',$(thisID).get('value')); }

Note that this code may not be 100% correct, as I'm writing it off the top of my head.

$("#rates") - JQuery first finds the form with the ID rates.  If the form is not identified as RATES each time, then just use a style descriptor and search for that, or some other identifier that uniquely identifies all forms for this code.

find("input.out") - iterates through all tags below the FORM tag for INPUT tags with the CSS style OUT.

each() - is a function that iterates through the list (Array) of tags returned by find.  It accepts a nested function which allows you to attach the onKeyUp event handler to this HTML tag.

If you need to pass additional values to your onKeyUp function, then you will need to nest that function further in another function ... blah blah blah ...

As you can see, I created a separate onKeyUp function, which takes 2 parameters.  These parameters would need to be passed through the nested function in each() ...  While I think I understand what outID is referring to, I am not sure what thisID is referring to.

Hopefully this gives you some ideas on how to attach the onKeyUp event handler to just the INPUT tags with a style class specifier of OUT. 

Mike,

--

On 2/5/2014 9:33 AM, Jack Drysdale Jr wrote:
Hello, everyone.

I've been working with JavaScript for a long time - nothing real fancy, mind you - but I'm scratching my head over this one.  I'm sure I'm missing something extremely simple.  I could use extra eyes, on this.

BTW, I'm in a DoD network, so sites like jsfiddle are blocked.  :(

I've got a form that will very in length each time it's loaded.   It's dynamically generated from a database query.  I'll try to keep the pseudo-code simple.

<form name="rates" id="rates">
  Rate 01: <input name="rate_A_B_1" class="in"><br />
  Rate 02: <input name="rate_A_C_1" class="in"><br />
  Rate 03: <input name="rate_C_A_1" class="out"><br />
  Rate 04: <input name="rate_A_D_1" class="in"><br />
  Rate 05: <input name="rate_D_A_1" class="out"><br />
  Rate 06: <input name="rate_A_E_1" class="in"><br />
  Rate 07: <input name="rate_E_A_1" class="out"><br />
  Rate 08: <input name="rate_A_F_1" class="in"><br />
  Rate 09: <input name="rate_A_G_1" class="in"><br />
  Rate 10: <input name="rate_G_A_1" class="out"><br />
    ...
  Rate 89: <input name="rate_A_Z_1" class="in"><br />
  Rate 90: <input name="rate_B_C_1" class="in"><br />
  Rate 91: <input name="rate_C_B_1" class="out"><br />
  Rate 92: <input name="rate_B_D_1" class="in"><br />
  Rate 93: <input name="rate_D_B_1" class="out"><br />
</form>


What I'm trying to do is (after the form loads, when the page is ready) add an onkeyup event to all "in" inputs that have a matching "out" input (ie, an "in" A_C_1 will get an onkeyup that will set the value of "out" C_A_1 to its value.  But I do not want "out"s to overwrite "in"s.. so apply only to "in" class inputs.)

So I thought, "Simple."  Here's my js.  Although I am using MooTools for some things, I'm mixing MooTools with straight JS for this.

var inInput = $$('.in'), x, thisID, thisIDary, elmnt;
for(x in inInput){
  if(typeof(inInput[x].id) !== 'undefined'){
    thisID = inInput[x].id.trim();
    thisIDary = thisID.split("_");
    outID = thisIDary[0] + "_" + thisIDary[2] + "_" + thisIDary[1] + "_" + thisIDary[3];
    outID = outID.trim(); outID = document.id(outID);
    $(thisID).addEvent('keyup',function(){
      $(outID).set('value',$(thisID).get('value'));
      }
    }
  }

Unfortunately, this will set a keyup event on all "in" class inputs that will set the associated "out" to the  value of the last "in" input of the form.

I'm sure I'm missing something simple.  Could someone please point out my error?

Thank you,

JD
--
You received this message because you are subscribed to the Google Groups "JavaScript Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to javascript-information+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


--
You received this message because you are subscribed to the Google Groups "JavaScript Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to javascript-information+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Copyright © 2010 Web Design | Free Blogger Templates by Splashy Templates | Layout by Atomic Website Templates

Vida de bombeiro Recipes Informatica Humor Jokes Mensagens Curiosity Saude Video Games Animals Diario das Mensagens Eletronica Rei Jesus News Noticias da TV Artesanato Esportes Noticias Atuais Games Pets Career Religion Recreation Business Education Autos Academics Style Television Programming Motosport Humor News The Games Home Downs World News Internet Design Entertaimment Celebrities 1001 Games Doctor Pets Net Downs World Enter Jesus Mensagensr Android Rub Letras Dialogue cosmetics Genexus lasofia thebushrajr wingshock tripedes gorduravegetal dainfamia dejavu-transpersonal jsbenfica republicadasbadanas ruiherbon iranianforaryans eaystcheyl fotosdanadir Só Humor Curiosity Gifs Medical Female American Health Madeira Designer PPS Divertidas Estate Travel Estate Writing Computer Matilde Ocultos Matilde futebolcomnoticias girassol lettheworldturn topdigitalnet Bem amado enjohnny produceideas foodasticos cronicasdoimaginario downloadsdegraca compactandoletras newcuriosidades blogdoarmario arrozinhoii