Alpha numeric generated values fails to insert into a texfield

i have auto generated alpha numeric values i generate. When i generate its able to pop up in alert, but its not able to insert the values into a textfield

function stringGen(len)
{
    var text = " ";

    var charset = "abcdefghijklmnopqrstuvwxyz0123456789";

    for( var i=0; i < len; i++ )
    text += charset.charAt(Math.floor(Math.random() * charset.length));

    return text;
    $('#search').val(stringGen(5)); 


}

alert(stringGen(5));

Answers 1

  • You are returning the function before the last jquery line runs. Try moving it up before the return and just putting text in the .val opposed to calling the function inside of itself.

      // define the function
      function stringGen(len)
      {
          var text = " ";
    
          var charset = "abcdefghijklmnopqrstuvwxyz0123456789";
    
          for( var i=0; i < len; i++ )
          text += charset.charAt(Math.floor(Math.random() * charset.length));
    
          // put the text in the jquery
          $('#search').val(text); 
      }
      // call the function here
      stringGen(5);
    

Related Articles