jQuery check and get attribute of a child element
If a user places HTML in a textarea
field, how can I check if text inserted is an <object>
tag?
If true, I want to assign the src
attribute of the <object
> to a variable.
What the most efficient way to do this?
<div id="embed">
<textarea>
<object src="..."></object开发者_StackOverflow中文版>
</textarea>
</div>
Try this...
var textarea = $('#embed textarea'),
textareaValue = textarea.val(),
textareaValueDom = $(textareaValue);
if (textareaValueDom.is('object')) {
var src = textareaValueDom.attr('src');
console.log(src); // http://example.com
}
jsFiddle.
var $obj = $($("#embed textarea").text());
var src = $obj.is("object") ? $obj.attr("src") : null;
fixed it
精彩评论