TommyT
TommyT

Reputation: 1797

Clicking on radio button does not issue the alert using jquery

On clicking the radio button nothing happens, why?

<html>
    <head>
        <link type="text/css" rel="stylesheet" href="css/bootstrap.css" />
        <script type="text/javascript" src="jquery.js">          
            $("input[name='check1']",$('#Search_By')).change(function()
            {
                alert('yo');
            });
        </script>
    </head>
    <body class="well">
        <form action="/abc/readservlet" method="post">
            <div class="well" id="Search_By">
                <h3><b/>Search BY</h3>
                <input type="Radio" name="check1" value="Search BY Key"> Key<br/><br/>
                <input type="Radio" name="check1" value="Search By Tablename"> Tab_name         
                <br/>
            </div>

On clicking radio button nothing happens...

Upvotes: 0

Views: 133

Answers (2)

Gokul Kav
Gokul Kav

Reputation: 849

The javascript code to create the onchange handler should be as follows. Try it out.

...
$("#Search_By input[name='check1']").change(
  function()
  {
    alert('yo');
});
...

Upvotes: 0

Blender
Blender

Reputation: 298066

If your <script> tag has a src attribute, its contents will be ignored, so your code isn't going to actually run at all.

You need to put your code into a separate script tag and run the code when the document is ready:

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $("#Search_By input[name='check1']")).change(function() {
            alert('yo');
        });
    });
</script>

Or put both of those script tags at the very bottom of the <body> tag.

Upvotes: 5

Related Questions