DUMB_CODER
DUMB_CODER

Reputation: 81

Push values into array jquery

I have created an empty array at the beginning of the form

<script>
   var monthly_values = [];
   var i=0;
</script>

Inside the loop trying to achieve below code.... but the push doesn't work without "alert" box nor if i remove "ready(function())"

$prodline=500$    /// CGI script loops till 500 iterations 
<script>
 $(document).ready(function(){
     i++;
     monthly_values.push("$premium$");
     setTimeout(function() { 
       alert("$premium$");
     }, 1);

 });
</script>
 $/prodline$//end of the iteration...

I push values from loop into array which is working .But i need to remove alert box or at least OK from alert box ..

The Entire Code is here..

    <html>
    <head>
      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>
       <title>PHP FILE  - $licensee$</title>
       <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">   
       <meta name="Author" content="COMPULIFE Software, Inc.">
       <link href="../../css/styles_website.css" type="text/css" rel="stylesheet">
       <link href="../../css/termsampler.css" type="text/css" rel="stylesheet">
       <link href="../../css/$css$" type="text/css" rel="stylesheet"> 
    <script>
         var monthly_values = [];
         var i=0;
    </script>

    <script language="JavaScript" type="text/javascript">
      //     var monthly_values = [];
         // var i=0;
    function getbest (ambest, company, compcode)
    {
      document.comparisonform.AmBest.value = ambest;
      document.comparisonform.CompanyName.value = company;
      document.comparisonform.CompanyCode.value = compcode;
      document.comparisonform.OnClick=abc=window.open('','reason','width=700,height=500,left=20,top=20,scrollbars=yes');
      document.comparisonform.target='reason';
      abc.focus();
      document.comparisonform.submit();
    }
    </script>  
    </head>
    <body>
    </tr>
    <style>
    .loader {
      border: 16px solid #f3f3f3;
      border-radius: 50%;
      border-top: 16px solid #3498db;
      width: 120px;
      height: 120px;
      -webkit-animation: spin 2s linear infinite;
      animation: spin 2s linear infinite;
      margin: auto;
      padding: 10px;
    }

    @-webkit-keyframes spin {
      0% { -webkit-transform: rotate(0deg); }
      100% { -webkit-transform: rotate(360deg); }
    }

    @keyframes spin {
      0% { transform: rotate(0deg); }
      100% { transform: rotate(360deg); }
    }
    </style>
    </head>
    <body>
    <div class="loader" style ></div>
    $prodline=500$

    <script>
      $(document).ready(function(){
        i++;
        monthly_values.push("$premium$");
        setTimeout(function() { 
          alert("$premium$"+"$company$"+"$product$"+"$healthcat$"+"$PremiumAnnual$"+"$Premium$"+"$rgpfpp$"+"$guar$"+"$policyfee$"+"$comp$"+"$prod$"+"$rowEvenOdd$");
        }, 1);

      });
    </script>
    $/prodline$
    </table>

    </form>

    <br><br>

    <script>
      $(document).ready(function(){

        var len=monthly_values.length;
        var min_quote=monthly_values [0];
          var max_quote=monthly_values [monthly_values.length-1];

        window.location = 'http://loalhost/laravel/public/customised-quote?min_quote_val='+min_quote+'&max_quote_val='+max_quote;
      });
    </script>

Upvotes: 1

Views: 737

Answers (1)

mike_t
mike_t

Reputation: 2691

This code should work without problems, check the snippet below. Are you including jQuery in your html?

var monthly_values = [];
var i=0;
 
 $(document).ready(function(){
     i++;
     monthly_values.push("$premium$");
     setTimeout(function() { 
      console.log(monthly_values)
     }, 1);

 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

EDIT after seeing your updated code, if i understand it right, in your loop you are rendering the the document $(document).ready() snippet 500 times, which is completely wrong approach.

Not sure what language you are using, and what exactly are you trying to achieve with your loop, but you could just print your $prodline$ array directly into javascript array variable without having to loop through it

Also, now i noticed in your $(document).ready() at the end of your file you are redirecting to new url immediately, if this is your intended result, generate the array just before you redirect like this:

<script>
$(document).ready(function(){
    var monthly_values = []

$prodline=500$
    monthly_values.push("$premium$")
$/prodline$

    var len=monthly_values.length;
    var min_quote=monthly_values [0];
    var max_quote=monthly_values [monthly_values.length-1];

    window.location = 'http://localhost/laravel/public/customised-quote?min_quote_val='+min_quote+'&max_quote_val='+max_quote;

})
</script>

Upvotes: 1

Related Questions