NG Miow
NG Miow

Reputation: 57

clear certain text using jquery

I face problem with reset value to empty with jquery when click radio button.

<form id="my-form">
<div>
    <label for="name">Name</label>
    <input type="text" id="name" name="name" value="123" />
</div>
<div>
    <label for="ID">ID NO</label>
    <input type="text" id="ID" name="ID" value="NO21034" />
</div>
 <fieldset>
        <legend>Option</legend>
        <label for="clearID">clearID</label> <input type="radio" id="clearID" name="opt" checked />
        <label for="clearName">clearName</label> <input type="radio" id="clearName" name="opt" />
 </fieldset>
</form>

<script type="text/javascript">
$(document).ready(function()
{
$('#clearname').on('click', function()
{ 
    $('#my-form').find('input:text').val('');
});
});
</script>

now if my radio button click clearName,it will automatic clear the value of name and id.
is it having another code can replace find('input:text') ?
I want to clear either one value(name or id),not both.

Upvotes: 0

Views: 1103

Answers (2)

use javascript reset method

$('#clearname').on('click', function()
{ 
    $("#my-form")[0].reset()
    // or
    $("#my-form").get(0).reset()
});

Upvotes: 1

Hikmat Sijapati
Hikmat Sijapati

Reputation: 6994

Clear each field using corresponding id..

<script type="text/javascript">
$(document).ready(function()
{
$('#clearname').on('click', function()
{ 
    $('#ID').val('');//it clears the value of element having id='ID'
    $('#name').val('');//it clears the value of element having id='name'
});
});
</script>

Upvotes: 2

Related Questions