MeAndMySelf
MeAndMySelf

Reputation: 173

Want to get set value in javascript issue

I want my code to display 170, 122 . All the values have been set in Javascript but still I am not getting the output.

<!DOCTYPE html>
<html>
   <body>
      <button onclick="UidToPost()">Get It</button>
      <script>
         var SetUid1 = "170";
         var SetUid2 = "122";
         var SetUid3 = "135";
         var SetUid4 = "144";

         var c = 0;
         var timingToSend;
         var SetUid;

         function UidToPost() {
            c = c + 1;
            var postTo = "SetUid" + c + "";
            document.getElementById("do").innerHTML = postTo;
            timingToSend = setTimeout('UidToPost()', 1000);
         };
      </script>
      <p id="do"></p>
   </body>
</html>

Thanks in advance.

This is the code that I am using

 var SetUid = [ "170", "122", "135", "144" ];

 var c = 0;
 var timingToSend;

 function UidToPost() {
    var postTo = SetUid[c++];
    document.getElementById("do").innerHTML = postTo;
    if (c < SetUid.length)          
       timingToSend = setTimeout(UidToPost, 1000);
 };

Upvotes: 1

Views: 69

Answers (5)

Brett Zamir
Brett Zamir

Reputation: 14345

document.getElementById("do").innerHTML = window[postTo];

You should also get in the habit of avoiding the string argument version of setTimeout as it can cause security issues:

timingToSend = setTimeout(UidToPost, 1000);

I presume you'll also want to call clearTimeout() (or avoid setting the last one in the first place), e.g., after your variables are finished.

Finally, this might have been done more easily and flexibly with an array, but the above is how you can do it with your current approach.

Upvotes: 1

Jamiec
Jamiec

Reputation: 136124

The reason this is not working, is that you're concatenating the string SetUid with the current count c to make a string, and adding that to the innerHTML of the div.

Instead, you should hold your values in an array and use your variable c as an index to that array:

     var values = [170,122,135,144]
     var c = -1;
     var timingToSend;
     var SetUid;

     function UidToPost() {
        c = (c + 1) % values.length; 
        var postTo = "SetUid" + c + "";
        document.getElementById("do").innerHTML = values[c];
        timingToSend = setTimeout('UidToPost()', 1000);
     }

Live example: http://jsfiddle.net/RsKZj/

Upvotes: 0

Alex K.
Alex K.

Reputation: 175826

Use an array instead of discreet variables;

     var SetUid = [ "170", "122", "135", "144" ];

     var c = 0;
     var timingToSend;

     function UidToPost() {
        var postTo = SetUid[c++];
        document.getElementById("do").innerHTML = postTo;
        if (c < SetUid.length)          
           timingToSend = setTimeout(UidToPost, 1000);
     };

Upvotes: 3

Abbas
Abbas

Reputation: 14432

This will do the trick:

document.getElementById("do").innerHTML = window[postTo];

You still have to perform a check for the number of vars. Now after the 4th var is displayed, the function will now write "undefined" to the screen because you keep looping.

Upvotes: 0

Anthony Grist
Anthony Grist

Reputation: 38345

You're trying to dynamically create the name of the variable to use with string concatenation, which is possible but not with that syntax. Since your variables are global variables, they'll be stored in the window object, so you can access them like this:

window["SetUid1"]
window["setUid2"]
//etc

With that in mind, you'll simply need to change this line:

var postTo = "SetUid" + c + "";

to:

var postTo = window["SetUid" + c];

You'll also need to handle the case where that variable doesn't exist (i.e. they click the button again after the last variable has been displayed), and take appropriate action (probably cycle back to the beginning).

Here is a working demo.

Upvotes: 1

Related Questions