0

I have seen this question and I have seen this fiddle.

My Fiddle here. It's a simple question. Why doesn't it work?

#html
<input checked="checked" id="article_format_html" name="article[format]" type="radio" value="html">Some meaningful value
<input id="article_format_text" name="article[format]" type="radio" value="text">Another Value

#js
$("input[name='article[format]']:radio").change(alert('hola'));

Edit:

On popular demand I did this:

.change(function(){alert('hola')});

Fiddle.

Result: Doesn't work.

Edit 2: (Why I had the problem)

So, JS-Fiddle wraps your js code in the head of the iframe that is your Result section. For jQuery selectors (or any js that manipulates the DOM) to work properly, it has to be executed *after* the DOM element has been rendered. Hence, wrapping your code on ready and/or just before body closes, is the safest way to ensure that your query selectors don't return undefined.

Community
  • 1
  • 1
manu29.d
  • 1,538
  • 1
  • 11
  • 15

11 Answers11

6
  1. Use a callback or anonymous function block
  2. You are using jQuery 1.11 (better to use .on syntax)

Demo: http://jsfiddle.net/abhitalks/e5ByP/2/

$("input[name='article[format]']:radio").on("change", function() { 
    alert('hola'); 
});
Abhitalks
  • 27,721
  • 5
  • 58
  • 81
  • hmmm...considering other answers, this one puzzles me. – manu29.d May 29 '14 at 11:24
  • @manu29.d: You don't have to change your choice of accepted answer. Rakesh's answer solves your problem so a change of choice wasn't required. – Abhitalks May 29 '14 at 11:29
  • @abhitalks but you want your answer to be accepted so OP did that. –  May 29 '14 at 11:31
  • All the other answers wrapped the handler on ready (| onDomReady | $(fucntion()..). Yours was wrapped in body. Can you make an edit and explain why both the methods work? And your syntax also `$("input[name='article\\[format\\]']:radio")` – manu29.d May 29 '14 at 11:33
  • @RickSmarty: Please don't distort the discussions. I never said and never wanted to get this accepted. On the contrary, it was me who said that it was Op's prerogative. – Abhitalks May 29 '14 at 11:33
  • 1
    @abhitalks there should be no discussions only you have started. Since OP has already marked the answer as accepted then why to change his mind? He marked because he tried Rakesh solution and it worked for him and this not show that your answer is wrong. So stay calm and enjoy the SO –  May 29 '14 at 11:36
  • @manu29.d This is the most unreadable format :))) If you really want to use `"` in the selector (as I do every time), use: `$('input[name="article[format]"]:radio').on();` Why to escape `"` when you can use `'` as delimiter? – vaso123 Feb 23 '17 at 10:49
  • @vaso123: I chuckled after seeing your comment. This question is almost 3 years old now. And this is most embarrassing thing I've posted on SO. I do use the syntax you mentioned. :) And I now realise that none of the answers explain why I had the problem. Which I understand now. :P – manu29.d Feb 26 '17 at 20:37
  • @manu29.d I know this feeling. Time to time I realize, how a newbee was I before X month, years. I started to develop in 1996-97, started with perl to build dynamic web pages. Until 8-9 years ago I've just used UltraEdit as a text editor, and know nothing about xDebug, IDE, used one monitor and spaghetti code etc... Now I can not imagine how could I work like that :))) – vaso123 Feb 27 '17 at 10:25
3

You code is not working because you are not wrapping your jQuery inside document.ready function:

$(document).ready(function()
{
   $("input[name='article[format]']:radio").change(function()
   {
       alert('hola')
   });

});

FIDDLE DEMO

Rakesh Shetty
  • 4,548
  • 7
  • 40
  • 79
2

The code with .on() should be:

$(document).on('change',"input[name='article[format]']:radio",function(){alert('hola')});

Working fiddle: http://jsfiddle.net/e5ByP/10/

Ghost-Man
  • 2,179
  • 1
  • 21
  • 25
1

Wrap that in a anonymous function and DOM ready:

$(function () {
    $("input[name='article[format]']:radio").change(function () {
        alert('hola')
    });
});

Demo:http://jsfiddle.net/e5ByP/6/

Amit Joki
  • 58,320
  • 7
  • 77
  • 95
1

That is because you have incorrect change handler syntax. also you need to wrap the code in DOM ready :

$("input[name='article[format]']:radio").change(function(){
  alert('hola')
});

Demo

Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
0

In your code you not wrap handler with .change()

handler is A function to execute each time the event is triggered.

Type: Function( Event eventObject )

Try This :

$("input[name='article[format]']:radio").change(function(){
  alert('hola')
});

Working Example

Ishan Jain
  • 8,063
  • 9
  • 48
  • 75
0

Please use anonymous callback function in change event

$("input[name='article[format]']:radio").change(function(){
  alert('hola');
});
Girish
  • 11,907
  • 3
  • 34
  • 51
0

Put the code in document ready and try like this. Please see the syntax too

$(function () {
    $("input[name='article[format]']:radio").change(function () {
        alert('hola')
    });
});
Mad Angle
  • 2,347
  • 1
  • 15
  • 33
0

put that in side document raedy

like this:

$(document).ready(function () {
$("input[name='article[format]']:radio").change(function(){
    alert('hola')
});
});

demo

Community
  • 1
  • 1
Awlad Liton
  • 9,366
  • 2
  • 27
  • 53
0

Use the below code

Always use DOM manipulation and events only after DOM gets fully loaded

$(document).ready(function(){
        $("input[name='article[format]']:radio").change(function(){alert('hola')});
    });
SimplyAzuma
  • 25,214
  • 3
  • 23
  • 39
Manimaran
  • 76
  • 1
  • 5
0

You forgot the $(document).ready() and also u forgot to write function() in change()

$(document).ready(function(){
    $("input[name='article[format]']:radio").change(function(){
        alert('hola')
    });
});
Sam
  • 41
  • 2
  • 13