Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jun 2013
    Thanked 0 Times in 0 Posts

    Javascript random button function

    When I click on the generate button, it generates a unique ID. When I click on it again it puts a different unique id in the same textbox. What should the code be to make it generate only 1 code in the textbox. and also generate a new code everytime i click on the button... in otherwords... everytime i click the button it should generate a new code in the textbox in replace of the other, not next to eachother in the same textbox.

    Supposed to look like this with a different code each time button is pressed.

    When I push on the generate button again it puts a new code behind the first. I would like to generate a new code in replace of the original without having it behind the first.

    function guidGenerator() {
    var S4 = function() {
    return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
    return (S4()+S4()+S4());


    function generateID() {

    var TheTextBox = document.getElementById("generateidtxt");
    TheTextBox.value = TheTextBox.value + guidGenerator();


  2. #2
    Master Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Behind the Wall
    Thanked 568 Times in 561 Posts
    remove the TheTextBox.value + from your function.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts