Ravi
Ravi

Reputation: 1

Unable to retrieve the value checked button in radio button group

I am trying to retrieve the value of the checked button in a radio button group.

    <script type="text/javascript">

function ButtonClicked() {

    alert($('input[name=datetype] :checked').val());
    return;
}

</script>

<form action="test.php">

<input type="radio" value="d1" name="datetype" checked onclick="javascript: ButtonClicked()">Date 1 <br />
<input type="radio" value="d2" name="datetype" onclick="javascript: ButtonClicked()"> Date 2 <br />

</form>

The output is always 'undefined' . I am a beginner to jQuery(and JS) so I may be missing something obvious but looking at tons of examples didn't help.

Upvotes: 0

Views: 363

Answers (3)

2red13
2red13

Reputation: 11227

much easyser:

<script type="text/javascript">
  function ButtonClicked(value) {
    alert(value);
    return;
 }

</script>
<form action="test.php">
<input type="radio" value="d1" name="datetype" checked onclick="javascript:ButtonClicked(this.value)">Date 1 <br />
<input type="radio" value="d2" name="datetype" onclick="javascript: ButtonClicked(this.value)"> Date 2 <br />
</form>

Upvotes: 0

r92
r92

Reputation: 2813

Remove 'javascript' from onclick.

<input type="radio" value="d1" name="datetype" checked onclick="ButtonClicked()">Date 1 <br />
<input type="radio" value="d2" name="datetype" onclick="ButtonClicked()"> Date 2 <br />

Upvotes: 0

benhowdle89
benhowdle89

Reputation: 37494

alert($('input[name=datetype]:checked').val()); try without a space

Upvotes: 1

Related Questions