Original Question
Is there a jQuery method that will check the selection type and set the value appropriately? Example, using .html() or .val().
I have made my own method to do the work for me however am not sure if this is the best way to preform this task.
$.fn.data = function(value) {
if(this.is("input")){
this.val(value);
}else if(... other type ...){
this.val(value);
}else{
this.html(value);
}
};
I have also looked at using the tagName attribute.
var type = this.prop(“tagName”).toLowerCase();
Edit:
Benchmarking
For anyone interested I did a little benchmarking between this.tagName; and $(this).is(“input”); with 10,000 records.
this.tagName.toLowerCase(); x 10,000 records = 185 milliseconds
$(this).is("input"); x 10,000 records = 1676 milliseconds
Here you go in plugin format:
Use it as follows:
See it here in action: http://jsfiddle.net/fkHLc/