Skip to content Skip to sidebar Skip to footer

How To Clear Text Inside Text Field When Radio Button Is Select

Currently I have this radio buttons Eletronics Computers Others What I am trying to do is, if radio button Others is selected, I would like to display an input text field and let

Solution 1:

UPDATE

$('input:radio').on('click', function() {
    if($(this).attr('id') == 'others') {
        $('#showhide').css('opacity', 1.0);
   } else {
       $('#showhide').css('opacity', 0).find('input:text#others_text').val('');
    }  
});​

DEMO


Solution 2:

You can use this:

document.getElementById("others_text").value='';

to clear the input field.


Solution 3:

Please add below code of line in your code:

document.getElementById("others_text").value = '';

now its look like:

document.getElementById("others").addEventListener("click", function()
{
    document.getElementById("others_text").value = '';
    document.getElementById("showhide").style.opacity = 1;
}, false);
document.getElementById("computers").addEventListener("click", function()
{
    document.getElementById("showhide").style.opacity = 0;
}, false);
document.getElementById("electronics").addEventListener("click", function()
{
    document.getElementById("showhide").style.opacity = 0;
}, false);​

This would be helpful for you.


Solution 4:

Try with this Answer :

$(function() {
      $('input[type="radio"]').click(function() {
       if($(this).attr('id') == 'others') {
           $('#others-text').show();
       } else {
           $('#others-text').hide();
           $('#others-text').val('');
       }
   });
})

Here there is a JSFiddle link:

http://jsfiddle.net/dnGKM/4/


Solution 5:

You add the jQuery tag to your question, so that should do the trick using jQuery :)

CSS:

.hidden {
    display: none;
}

HTML:

<label for="electronics">Electronics</label>
<input type="radio" id="electronics" name="rdbutton" />
<label for="computers">Computers</label>
<input type="radio" id="computers" name="rdbutton" />
<label for="others">Others</label>
<input type="radio" id="others" name="rdbutton" />
<input type="text" id="others-text" name="others-text" class="hidden" />

JS:

$(function() {
    $('input[type="radio"]').click(function() {
        if($(this).attr('id') == 'others') {
            $('#others-text').removeClass('hidden');
        } else {
            $('#others-text').addClass('hidden');
            $('#others-text').val('');
        }
    });
});

Post a Comment for "How To Clear Text Inside Text Field When Radio Button Is Select"