Loading ...
Sorry, an error occurred while loading the content.
 

Re: [JavaScript] passing a var result to use as a var

Expand Messages
  • hotpop
    dude this is so what i want... i am just learning JAVASCRIPT and didn t know about eval(), there is a similar function in cold fusion and am use to using it
    Message 1 of 4 , Mar 6, 2003
      dude this is so what i want... i am just learning JAVASCRIPT and didn't know about eval(), there is a similar function in cold fusion and am use to using it and understanding it.... so.. this is what i needed. thank you for your time and for understanding my confusion... also accept my applologies if i seemed a little snippy .. thank you all again... oh yeah i haven't tried this yet but when i get the chance I will try it out... I am in the middle of rewriting the page that uses this and it is about 600 lines long...

      ----- Original Message -----
      From: David Smart
      To: javascript_official@yahoogroups.com
      Sent: Thursday, March 06, 2003 8:25 AM
      Subject: Re: [JavaScript] passing a var result to use as a var


      You have not been explaining what you want very clearly.

      However, I think you are saying something like ...

      You have variables called

      aa
      bb
      cc

      each containing something useful, now the user will somehow put a value into xx. This value will be a string "aa" or "bb" or "cc". You then want to use this to copy the value from the appropriately-named variable to yy.

      aa = something useful;
      bb = something else useful;
      cc = something else again;

      xx = user input (only aa or bb or cc);

      yy = eval (xx);

      If you want to use a string in a variable as the name of a variable (or part of a name or whatever) use eval.

      Note that you can't simply reverse this to set something into these variables. I.e.

      eval (xx) = good stuff;

      fails. However,

      eval (xx + ' = ' + text representation of good stuff, with quotes if needed);

      is fine.

      Note that using a similar style

      eval ('yy = ' + xx);

      you can do the same as

      yy = eval (xx);

      Your choice of which is easier to read. No contest in my eyes.

      Try to avoid this sort of selection-by-name coding wherever possible though - it's error prone, and harder to read than normal code. Use arrays when you need to select individual items out of sets of items, unless arrays just can't be used.

      Also, make the absolute minimum evaluation. For instance, you're selecting between getit1 and getit2. Do this by having the user put a 1 or 2 in an integer (call it zz) and doing:

      yy = eval ('getit' + zz)

      At least, now, it's only the index which is being supplied, not the full name of the variable.

      Regards, David Smart

      SmartWare Consulting
      smartware@...
      http://members.optushome.com.au/smartware/

      ----- Original Message -----
      From: hotpop
      To: javascript_official@yahoogroups.com
      Sent: Thursday, March 06, 2003 11:41 PM
      Subject: Re: [JavaScript] passing a var result to use as a var


      Can no one help me with this? I would think someone could figure this out. I don't know how to take an output of a variable to use as a name of a variable. Is this possible? I know i can do it with cold fusion but that's server side.. I need something client side.

      ----- Original Message -----
      From: hotpop
      To: javascript_official@yahoogroups.com
      Sent: Wednesday, March 05, 2003 11:44 AM
      Subject: Re: [JavaScript] passing a var result to use as a var


      here is some better code to look at hopefully

      <script>

      var getit1='l wejflwejlkf wmelr welk rmewlrlkwe ml,l ldflasdmlsam dklsad masmdasda lasmdl am ld';

      var getit2='asdasdasdsa, asdasdasdsdn fdsnfjkdsn fndsjf nsdk fndsn fksdnfasdasd as das kjsdn fk';

      function getit()

      {

      var sumindex=document.courseSel.subclass_id.selectedIndex;

      var sumid=document.courseSel.subclass_id.options[sumindex].id;

      alert(sumid);

      }

      </script>


      <select name="subclass_id" size="6">

      <option value="XX">--- Choose an Applicable Problem Summary ---</option>

      <option value="123" id="getit1">l wejflwejlkf wmelr welk rmewlrlkwe ml,l ldfla...</option>

      <option value="4534" id="getit2">asdasdasdsa, asdasdasd...</option>

      </select>

      <br>

      <input type="button" onclick="getit();" value="Click for full">
      ----- Original Message -----
      From: hotpop
      To: javascript_official@yahoogroups.com
      Sent: Wednesday, March 05, 2003 11:42 AM
      Subject: [JavaScript] passing a var result to use as a var


      Hi there,

      I need help with the below code... I wan to get the id result of an option from a select box and use that result as a var caller.. if caller is right... so lets say the option id is get1 i have a variable with the name of get1 and when i return the results of the option it calls that option as a var into an alert... see below...


      ================================================================================
      <script>var getit1='l wejflwejlkf wmelr welk rmewlrlkwe ml,l ldflasdmlsam dklsad masmdasda lasmdl am ld';var getit2='asdasdasdsa, asdasdasdsdn fdsnfjkdsn fndsjf nsdk fndsn fksdnfasdasd as das kjsdn fk';function getit(){var sumindex=document.courseSel.subclass_id.selectedIndex;var sumid=document.courseSel.subclass_id.options[sumindex].id;alert(sumid);}</script><select name="subclass_id" size="6"><option value="XX">--- Choose an Applicable Problem Summary ---</option><option value="123" id="getit1">l wejflwejlkf wmelr welk rmewlrlkwe ml,l ldfla...</option><option value="4534" id="getit2">asdasdasdsa, asdasdasd...</option></select><br><input type="button" onclick="getit();" value="Click for full">================================================================================

      [Non-text portions of this message have been removed]


      Yahoo! Groups Sponsor
      ADVERTISEMENT




      To unsubscribe from this group, send an email to:
      javascript_official-unsubscribe@yahoogroups.com



      Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.


      [Non-text portions of this message have been removed]


      Yahoo! Groups Sponsor
      ADVERTISEMENT




      To unsubscribe from this group, send an email to:
      javascript_official-unsubscribe@yahoogroups.com



      Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.


      [Non-text portions of this message have been removed]


      Yahoo! Groups Sponsor





      To unsubscribe from this group, send an email to:
      javascript_official-unsubscribe@yahoogroups.com



      Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.


      [Non-text portions of this message have been removed]


      Yahoo! Groups Sponsor
      ADVERTISEMENT




      To unsubscribe from this group, send an email to:
      javascript_official-unsubscribe@yahoogroups.com



      Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.


      [Non-text portions of this message have been removed]
    Your message has been successfully submitted and would be delivered to recipients shortly.