Javascript: Set value from multiple field using 1 function but different values

Help please, my problem is, when i set a value from multiple text field by calling 1 function it duplicates the value. How do i make this different to each other. pardon for my English.

Here is my code

//code generate
function guid() {
    return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
    s4() + '-' + s4() + s4() + s4();
}

function s4() {
    return Math.floor((1 + Math.random()) * 0x1000)
   .toString(16)
   .substring(1);
}
var uuid = guid();

$('input:radio[name="judge_option"]').change(function (){
   var j_radio = $(this).val();
   if(j_radio == 'backdoor_access'){
        $('.backdoor_link').val(uuid); //<---- when i fire the trigger 
   }
});

The output shows like this

<input type="text" class="backdoor_link" value="ae668f-2bf-a24-6a5-58677a007"> 
<input type="text" class="backdoor_link" value="ae668f-2bf-a24-6a5-58677a007"> 
<input type="text" class="backdoor_link" value="ae668f-2bf-a24-6a5-58677a007">
<input type="text" class="backdoor_link" value="ae668f-2bf-a24-6a5-58677a007">

How do i make it look like this one, the values that i set should be different to each other in 1 click.

<input type="text" class="backdoor_link" value="143e37-cb3-b83-4ef-aca186f3d"> 
<input type="text" class="backdoor_link" value="f3e198-b1c-918-28c-00e29debb"> 
<input type="text" class="backdoor_link" value="bda03e-4e0-da7-f82-36592439c">
<input type="text" class="backdoor_link" value="e73b85-3f0-220-9ec-acffa6ba8">

Upvotes: 2

Views: 99

Answers (1)

billyonecan
billyonecan

Reputation: 20250

You're only calling guid() once, and you're assigning it to all .backdoor_link elements. Pass the function to val() so that it's called for each .backdoor_link:

$('.backdoor_link').val(guid);

Here's a fiddle

Upvotes: 3

Related Questions