Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript How I can adjust the string output in JavaScript and give a sequence

irfanjamal

New Coder
I need help, I am using Javascript code to detect Radios, TextInputs and Text Area fields and push the detetcted values from field to a sequence in a input field. I've given each field a id with sequence, name1, name2, name3, name4, name5, name6, name7, name8, name8, name10, and name11 and the field id where string is pushied is sc_tgenerator_form_field_prompt_text. I have some code, and wondering if its possible to adjust the sequence of final string that is pushed in sc_tgenerator_form_field_prompt_text
I've tried this code, and this is working. it shows radios values sequencely but it shows all textinputs and textareas at last of string. its not correct sequence correct sequence is the name 1, 2, 4, 5, 9 are radios and name 3, 6, 7 8 are text boxes, and 10 and 11 are text areas Here is code

JavaScript:
// Select all radio buttons from the nine forms
const radios = document.querySelectorAll('input[type="radio"]');

// Select the input field where you want to display the generated string
const inputField = document.querySelector('.sc_tgenerator_form_field_inner input[type="text"]');

// Store text input values separately
let textInputValues = {};

// Function to update the input field based on selected radio options
var defaultTextGlobal;
var counter = 0;

function updateInputField() {
    if (counter == 0) {
        defaultTextGlobal = jQuery('.sc_tgenerator_form_field_prompt_text').val() + ',';
    }

    let selectedOptions = [];
    radios.forEach(radio => {
        if (radio.checked) {
            selectedOptions.push(radio.value);
        }
    });

    // Include text input values in the selected options
    Object.keys(textInputValues).forEach(key => {
        if (textInputValues[key].trim() !== '') {
            selectedOptions.push(textInputValues[key].trim());
        }
    });

    counter = counter + 1;
    inputField.value = defaultTextGlobal + ' ' + generateString(selectedOptions);
}

// Generate the string with the desired format
function generateString(options) {
    if (options.length === 0) {
        return '';
    }
    let result = options[0] + ' of ';
    if (options.length > 1) {
        result += options.slice(1).join(', ');
    }
    result = result.replace('Do not include of ', '');
    result = result.replace('Do not include', '');
    result = result.replace('Do not include of,', '');
    result = result.replace('Do not include,', '');

    return result;
}

// Add event listeners to all radio buttons to listen for changes
radios.forEach(radio => {
    radio.addEventListener('change', updateInputField);
});

// jQuery for text inputs and text areas
jQuery(document).ready(function ($) {
    // Select text inputs and text areas and add input event listener
    $('#form-field-name3, #form-field-name6, #form-field-name7, #form-field-name8, #form-field-name10, #form-field-name11').on('input', function () {
        textInputValues[this.id] = $(this).val(); // Store text input values
        updateInputField(); // Update the input field
    });
});
 
I think what you could do instead of making one string, make an array of strings with what you desire for each thing then get desired format by concatenating based on which should be where based on order you get them in. Maybe you can keep radios foreach where it is with that but I would have it when jQuery ready HTML before other things. You can use .push and .unshift if you have a dynamic number of them. X E.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom