arma
arma

Reputation: 4124

Why this Jquery code does not run?

I have Jquery code that checks on .change status of HTML select element and when there's change in select element it should check if certain radio button is active.

 $('#select_ele').change(function(){
     //
     //... Doing all kind of fancy stuff here
     //
     if($('input[name="radio_button"]').val() == 'bp'){
         alert('YO'); 
     }
 });

So the idea here was on change of select check if 'bp' radio button is active and if so then alert me.

Upvotes: 1

Views: 101

Answers (4)

user113716
user113716

Reputation: 322462

Your code will always give you the .val() of the first name="radio_button" match it finds, whether or not it is checked.

Instead, you should use the checked-selector(docs) to get the one that is checked.

$('input[name="radio_button"]:checked').val() == 'bp'

Upvotes: 4

ChuckJHardy
ChuckJHardy

Reputation: 7066

Please this in the middle, it will help you work it out.

alert("Hello I am Working")

As well as this.

console.log(event.type);

Upvotes: 0

keithjgrant
keithjgrant

Reputation: 12739

Think this is what you're looking for:

    if($('input[name="bp"]').is(":checked"){ alert("yo"); })

Upvotes: 0

SLaks
SLaks

Reputation: 887285

Your code is probably running before the document loads.
Therefore, the selector isn't matching anything (since the document hasn't been parsed yet), and the script doesn't do anything Move the <script> block to the bottom of the page or wrap it in $(function() { ... }) (this executes it in the document load event).

Upvotes: 2

Related Questions