tiny jQuery plugin to get text from the first elem
HTML
<label>Name <input type="text" class="requiredField" name="name" /><span class="required">Required</span></label>
CSS
body { padding: 30px; }
.output {
padding: 30px;
margin: 80px 20px;
border: 1px solid #666;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
}
.output:before {
color: green;
opacity: 0.5;
}
.first-text:before { content: 'First Text:\2003'; }
.text:before { content: 'Full Text:\2003'; }
JavaScript
jQuery.fn.firstText = function() {
return this
.clone()
.children()
.remove()
.end()
.text();
};
jQuery('label').each(function() {
var $thisElem = jQuery(this);
jQuery('<div>', {
class: 'output text'
})
.appendTo(jQuery('body')) // Create an Output div and append to body
.append(
$thisElem.prev().text() // text()
);
jQuery('<div>', {
class: 'output first-text'
})
.appendTo(jQuery('body')) // Create an Output div and append to body
.append(
$thisElem.prev().firstText() // our plugin
);
});