1

I hav a 5 radios and when it loads it runs

$("input[type='radio'][name='freq']")[1].trigger('change');

to activate the below function

(function() {
$("form#form1 input, form#form1 select").on('keyup change', function() {
alert('called')
}):
}).call(this);

But trigger('change') will not neither trigger('click')

1
  • Needs to be a JQuery solution? Commented Apr 16, 2017 at 10:48

2 Answers 2

2

Instead of this:

$("input[type='radio'][name='freq']")[1].trigger('change');

try

$("input[type='radio'][name='freq']")[1].click();

It will make the checkbox checked if it is unchecked, or vice versa. You can also use:

$("input[type='radio'][name='freq']")[1].attr('checked', true);
$("input[type='radio'][name='freq']")[1].attr('checked', false);

or

$("input[type='radio'][name='freq']")[1].prop('checked', true);
$("input[type='radio'][name='freq']")[1].prop('checked', false);
Sign up to request clarification or add additional context in comments.

6 Comments

No i am not, this $("input[type='radio'][name='freq']")[1].attr('checked', true); $("input[type='radio'][name='freq']")[1].attr('checked', false); is not a function
I did not get you?
Uncaught TypeError: $(...)[1].prop is not a function
$("input[type='radio'][name='freq']")[1].click(); will click the radio but does not fire
Try this one: $("input[type='radio'][name='freq']")[1].click().change();
|
1

document.getElementById("myBtn").addEventListener("click", functio_test);
document.getElementById("myBtn1").addEventListener("click", functio_test);

        function functio_test(){
            var x = document.querySelector('input[name="type_test"]:checked').value;
            if(x == "ola"){
                 alert("Ola José");
            }
            else if(x == "adeus"){
                 alert("Adeus José");
            }
            else {
                alert("Continua José");
            }
        }
<input id = "myBtn" type="radio" name="type_test" value="ola"> ola <br/>
    <input id = "myBtn1" type="radio" name="type_test" value="adeus"> adeus <br/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.