vendredi 21 octobre 2016

How can I get an if conditional do only run once in onclick() function?

I'm writing in to see if I can hopefully get some help here.

I'm using pure javascript and I have a link that looks like this:

<li><a class="cube_size = cube_size<cms:show cube_size_code />" href="javascript:void(0)" 
    onclick="update_price('
    <cms:show cube_size_code />-
    <cms:show cube_size_weight />-
    <cms:show cube_size_price />-
    <cms:show cube_size />-
    <cms:show cube_size_price_2/>')">
    <cms:show cube_size /></a></li>

As you can see it has a onclick event called update_price() there's four li's with the same code but of course with different data pulled into the string from the CMS which btw is CouchCMS.

What I'm trying to do is get the code in the if statement to run only once, what's happening is every time I click the different links the function starts back over which means the do while will start back over:

var counter = 0;
function update_price(exp){
    var cube_options = exp.split('-');
    var cube_size_code = cube_options[0];
    var cube_size_weight = cube_options[1];

    do{
    counter++;
    if(cube_size_code == "FFRK"){
      cube_size_code = "GHIEQ";  
      }while(counter < 1);
     }
    }

I would greatly appreciate if you can provide a simple technique on how I can prevent the do-while from resetting on every click or besides a loop is there a way for the if block to execute only once? PLEASE HELP!

Aucun commentaire:

Enregistrer un commentaire