Reputation: 958
I am trying to figure out if there is already a function to do the following or how would I write a new one if the page is editable, i build a table with text boxes in it.
<td><input type="text" id="blah" ... > </td>
otherwide:
<td id="blah">Sometext</td>
and then I build another table based on values from this table.
To get the value of the input box in jquery i do:
$('#blah').val()
otherwise
$('#blah').text()
I want to write a function that based on the flag (isEditable) return the elements .val or .text for example
jquery.fn.getValue = function() {
if (isEditable){
return $(this).val()
}
else {
return $(this).text()
}
}
and then I would be able to do:
$('#blah').getValue()
and I wouldn't care whether its a cell or input box
Upvotes: 2
Views: 159
Reputation: 15163
You were basically already there:
(function($) {
$.fn.getValue = function() {
switch(this.get(0).nodeName) {
case"INPUT": return this.val();
break;
default: return this.text();
break;
}
}
})(jQuery);
Upvotes: 3
Reputation: 4183
you could use the this.tagName
to see if the element is an input or a td and return the value or the text according to that
Upvotes: 0