javascript - get element type from element found by class -
im trying create function "value" of type of element has specific class (textarea, span,... whatever)
to need test kind of element im dealing can enough alert($('#gdocdump').prop('tagname'));
reason if grab elements of same class var varelems=$(".feedback");
, try loop through them testing each 1 var elementtype = elem.prop('tagname');
, error "typeerror: elem.prop not function"
- why happen?
- apparently elements stored in jquery object created $(".feedback") not quite same looking @ each individually?
- what need change
var elementtype = elem.prop('tagname');
work below?
jsfiddle:testing ground
html
<textarea id="gdocdump" class="feedback area" rows="1" cols="22" ></textarea><br /> <input id="scaleslider" class="feedback" type="range"value="1" min="1" max="9" step="1"/><br /> <span id="command1" class="feedback">this span</span><br /> <span id="command2" class="feedback">this span</span><br /><br /> <input type="button" id="sendfeedback" value="feedback"/><br /><br /><br /><br /> <input type="button" id="test" value="test specific element"/>
javascript:
$("#sendfeedback").click(function() { composeandcallemail(); }); $("#test").click(function() { alert($('#gdocdump').prop('tagname')); }); function composeandcallemail() { var varelems=$(".feedback"); var feedback=[]; $(varelems).each(function(index, element) { feedback.push(getelemcontents(element)) }); } function getelemcontents(elem){ var elementtype = elem.prop('tagname'); //this problem occurs if(elementtype=='input')return elem.val(); //... add more here later }
inside each(index, element)
arguments index , native dom element
$(varelems).each(function(index, element) { feedback.push(getelemcontents(element)) // plain dom element, not jquery });
that means have wrap again or use native elem.tagname
property
function getelemcontents(elem){ var elementtype = $(elem).prop('tagname'); if ( elementtype.tolowercase() === 'input' ) return $(elem).val(); }
Comments
Post a Comment