$(this).attr("id") Not Working
Solution 1:
Change
varID = $(this).attr("id");
to
varID = $(obj).attr("id");
Also you can change it to use jQuery event handler:
$('#race').change(function() {
    varselect = $(this);
    var id = select.attr('id');
    if(select.val() == 'other') {
        select.replaceWith("<input type='text' name='" + id + "' id='" + id + "' />");
    } else {
        select.hide();
    }
});
Solution 2:
your using this in a function, when you should be using the parameter.
You only use $(this) in callbacks... from selections like
$('a').click(function() {
   alert($(this).href);
})
In closing, the proper way (using your code example) would be to do this
obj.attr('id');
Solution 3:
Because of the way the function is called (i.e. as a simple call to a function variable), this is the global object (for which window is an alias in browsers). Use the obj parameter instead.
Also, creating a jQuery object and the using its attr() method for obtaining an element ID is inefficient and unnecessary. Just use the element's id property, which works in all browsers.
functionshowHideOther(obj){ 
    var sel = obj.options[obj.selectedIndex].value;
    varID = obj.id;
    if (sel == 'other') { 
        $(obj).html("<input type='text' name='" + ID + "' id='" + ID + "' />");
    } else {
        $(obj).css({'display' : 'none'});
    }
}
Solution 4:
You could also write your entire function as a jQuery extension, so you could do something along the lines of `$('#element').showHideOther();
(function($) {
    $.extend($.fn, {
        showHideOther: function() {
            $.each(this, function() {
                varId = $(this).attr('id');
                alert(Id);
                ...
                returnthis;
            });
        }
    });
})(jQuery);
Not that it answers your question... Just food for thought.
Solution 5:
What are you expecting $(this) to refer to?
Do you mean sel.attr("id"); perhaps?
Post a Comment for "$(this).attr("id") Not Working"