Mitchel Stuart Fountaine Mitchel Stuart Fountaine - 7 months ago 10
Javascript Question

Remove Div Attribute when radio button is selected

I'm very new to JavaScript, not really sure what I'm doing. I found some useful code that removes/inserts a Div's attributes when a certain value is selected from a dropdown list. I'd like to modify this code so that the attribute is altered when a radio button is selected.

Here is the code I have at the moment:

function pageLoad(sender, args) {
if (args.get_isPartialLoad()) {
var rangeType = $('#<%= DateRangeList.ClientID%>').val();
if (rangeType == 6) {
$('#DatePeriodRow').removeAttr("style");
}
else {
$('#DatePeriodRow').attr("style", "display: none;");
}
}
}


$(document).ready(function () {
$('#<%= DateRangeList.ClientID %>').live("change", function() {
var rangeType = $('#<%= DateRangeList.ClientID %>').val();
if (rangeType == 6) {
$('#DatePeriodRow').removeAttr("style");
}
else {
$('#DatePeriodRow').attr("style", "display: none;");
}
});
})


I've been trying various ways of rebuilding this code to suit my needs but no luck thus far. I've also done quite a bit of Googling to no avail. I often receive the "Propery/Value is null or undefined" message, and I'm not to sure how to go about debugging the java side of things.

Any and all help would be greatly appreciated!

Answer

If you're trying to show hide and element, Raki's answer will do. You could also express it this way:

$('#DatePeriodRow').hide(); // Hide Div
$('#DatePeriodRow').show(); // Show Div

First things first though, if your Property/Value is undefined or null you probably have a problem with your selectors. I.e. the following line:

$('#<%= DateRangeList.ClientID %>')

The <%= ... %> looks like ASP, if this is intended to be run inside that environment perhaps that code is not evaluating correctly which is giving you the wrong jQuery selector.

One More Thing

Check which version of jQuery you're using, .live() is deprecated, newer versions use .on