Wednesday, 15 July 2015

jquery - jQuerify String To Find Element Fails -



jquery - jQuerify String To Find Element Fails -

there string textarea wrap jquery , utilize 'find()' search element:

var info = '<div class="page-content-wrapper"><div class="page-content"><div class="row"><div class="col-md-12"><ul class="page-breadcrumb breadcrumb editor-wrapper-end">content comes here</ul></div></div></div></div>'; var jdata = jquery(data); jdata.find('.editor-wrapper-end').length == 1

this works, fails, when info contains more 1 sibling:

var info = '<div class="page-header navbar navbar-fixed-top>top menu</div><div class="page-content-wrapper"><div class="page-content"><div class="row"><div class="col-md-12"><ul class="page-breadcrumb breadcrumb editor-wrapper-end">content comes here</ul></div></div></div></div>'; var jdata = jquery(data); jdata.find('.editor-wrapper-end').length == 0//

the above fails find element though element class '.editor-wrapper-end' exists. 1 of solutions, thinking wrap info between body tags before jqueryfy(i.e. jquery('' + info + '')). doesn't work because jquery() still returns array of children. best solution here? prefer avoid handling loop(.each) myself because ultimately, remove tags before , after element class editor-wrapper-end, handling looping through each children sounds lot of work me. ...i phone call jdata('editor-wrapper-end') , done. give thanks you

here illustration @ http://jsfiddle.net/bf6wtlav/1/

the problem having due fact html searching has element searching @ root level. jquery's find() searches matching kid elements of top-level elements. take example...

$("<div class='mydiv'></div>").find(".mydiv");

that homecoming empty array since find() looks inside top-level div, in case div we're looking for.

this, however, homecoming want...

$("<div class='top-level'><div class='mydiv'></div></div>").find(".mydiv");

.find() searches inside div.top-level, , time finds we're looking for.

so, wrapped html within div in illustration , returns you're looking for...

http://jsfiddle.net/bf6wtlav/11/

an alternative utilize .each() , check each element see if has corresponding class. longer, , more code, means don't have alter initial html, guess may more preferable. without context it's hard know, utilize suits needs best...

http://jsfiddle.net/bf6wtlav/12/

jquery

No comments:

Post a Comment