Iterate text in label by clicking button

This forum contains topics that were moved from Get Satisfaction
Peter LPunkt
Posts: 0
Joined: Mon Jan 13, 2014 3:07 pm

Iterate text in label by clicking button

Post by Peter LPunkt »

Hi there,

i want to do something pretty simple, but can't figure out, what i am doing wrong. I have a mobilelabel ('mobilelabel_37') and a button. I want to iterate through different textoutputs when clicking the button.

I tried this js on click button:

code
var i=0;

if (i===0)
{
Appery('mobilelabel_37').html('Textoutput 1 bla bla');
}

else if (i==1)
{
Appery('mobilelabel_37').html('Textoutput 2 bla bla');
}

else if (i==2)
{
Appery('mobilelabel_37').html('Textoutput 3 bla bla bla');
}

i++;

/code
Cheers Peter.

Evgene Karachevtsev
Posts: 12
Joined: Mon Apr 28, 2014 1:12 pm

Iterate text in label by clicking button

Post by Evgene Karachevtsev »

Hello Peter,

Most likely this happens because you with every click every time reset variable i ( var i=0; )
You may add a variable in the local storage and work with it and increase its value

Peter LPunkt
Posts: 0
Joined: Mon Jan 13, 2014 3:07 pm

Iterate text in label by clicking button

Post by Peter LPunkt »

Could you give me a brief description, how something like this could be implemented with a JSON or XML file? I would have to create a generic service, right?

Evgene Karachevtsev
Posts: 12
Joined: Mon Apr 28, 2014 1:12 pm

Iterate text in label by clicking button

Post by Evgene Karachevtsev »

Peter,

When the page loads please run js code that will create the variable.
prelocalStorage.setItem('inc', 0);/pre
When you click on the button, start your modified code:
prevar inc = parseInt( localStorage.getItem('inc'), 10 );

switch ( inc ) {
case 0: Appery('mobilelabel_37').html('Textoutput 1 bla bla');
break;
case 1: Appery('mobilelabel_37').html('Textoutput 2 bla bla');
break;
case 2: Appery('mobilelabel_37').html('Textoutput 3 bla bla bla');
break;
}

localStorage.setItem('inc', inc + 1 );/pre

Peter LPunkt
Posts: 0
Joined: Mon Jan 13, 2014 3:07 pm

Iterate text in label by clicking button

Post by Peter LPunkt »

Hi there,
thanks :) works the way i want.
Here is the code i used, in case anyone is interested:

Set the localstorage counter on page load:
pre
localStorage.setItem('counter', 0);
/pre

I made a modification, so that only the five messages are iterated (if condition at the end, so counter cant get bigger than 4)
pre
var counter = parseInt( localStorage.getItem('counter'), 10 );
switch ( counter ) {
case 0: Appery('mobilelabel_37').html('Message output 1');
break;
case 1: Appery('mobilelabel_37').html('Message output 2');
break;
case 2: Appery('mobilelabel_37').html('Message output 3');
break;
case 3: Appery('mobilelabel_37').html('Message output 4');
break;
case 4: Appery('mobilelabel_37').html('Message output 5');
break;
}

if (counter = 4){
var counter = -1;} /-1 because of the +1 added by localstorage.setitem, so case 0 is shown./

localStorage.setItem('counter', counter + 1 );
/pre

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

Iterate text in label by clicking button

Post by Kateryna Grynko »

Hi Peter,

Thank you for sharing it!

Post Reply