Search code examples
javascripthtmltextboxradio-buttononblur

Javascript OnBlur not working


I have a textbox which is disabled by default... the onclick works and enables it to be typed in, but when i click the radio button away.. it doesn't disable the textbox again

Any clues ?

<input type="radio" name="group1" value="website" OnBlur="javascript:document.getElementById('websiteurl').disabled = true;" OnClick="javascript:document.getElementById('websiteurl').disabled = false;">
<input name="websiteurl" type="text" id="websiteurl" value="http://" size="50" disabled="true" />

Thanks in advance

Lee


Solution

  • There is no attribute enabled. You use disabled and set it to true or false.

    Besides that:

    Use lowercase attributes (otherwise it's not valid XHTML); there's no need to put javascript: in front of it. Additionally, it is better to attach events from JavaScript code instead of inlining them. Especially if you use a library like jQuery it's extremely easy:

    $('input[name=group1][value=website]').blur(function() {
        $('#websiteurl').attr('disabled', 'true');
    }).click(function() {
        $('#websiteurl').removeAttr('disabled');
    });
    

    (of course a #id would be much better instead of the input[name=group1][value=website] selector)