How I call a function like this in Javascript/jquery?

Panther28

Elite Member
Executive VIP
Jr. VIP
Joined
May 2, 2010
Messages
9,937
Reaction score
16,034
It show you just how much a clue I have about javascript, I'm not actually sure if this is vanilla or jquery?

Anyway the code I have is

JavaScript:
  $(document).on('ready',prepare() {
    $(document).on('change',prepare() {

    })

    function prepare {
        const checkbox = document.querySelector('input[name="timescale_radio_buttons"]:checked').value;
        document.getElementById('output-2').innerHTML = "Time: " + checkbox;
        }

    })

I need the function to run on ready, and then when there is a change, can anyone tell me how I do that here?


Cheers
 
It show you just how much a clue I have about javascript, I'm not actually sure if this is vanilla or jquery?

Anyway the code I have is

JavaScript:
  $(document).on('ready',prepare() {
    $(document).on('change',prepare() {

    })

    function prepare {
        const checkbox = document.querySelector('input[name="timescale_radio_buttons"]:checked').value;
        document.getElementById('output-2').innerHTML = "Time: " + checkbox;
        }

    })

I need the function to run on ready, and then when there is a change, can anyone tell me how I do that here?


Cheers
What a noob, how can I have an on 'click' on a document, anyway, I think I fixed it, with

JavaScript:
 $(document).on('ready',function() {
        prepare();
    $("#24Hours").on('click',function() {
        prepare();
    });
    
    $("#3Days").on('click',function() {
        prepare();
    });
                                        
});

Tested and works ok.
 
Anything with a $ is jquery. We don't use it in Vanilla. If it's already working for you, I guess you defined prepare() somewhere else.
 
You don't need jQuery in this day and age, if you want multiple selections in a group, instead of applying an eventListener to each checkbox, try the formData function, mozzila has a good section on this.

I used some of the example to show you, you can have as many options as you want, inside of the form, and you can store the value entry[1] as some data and use it wherever you want:

https://jsfiddle.net/procoib/6wr3d27k/
 
Back
Top