Replace an html element with it's index via jQuery | js jquery replace element
$(.resultsdiv).eq(index).replaceWith(html);Fiddle[1]Youshoulduse.eq()[2]tofilterthesetofmatchedelementsinsideajQueryobjectbyitsindex.Also$(.results)onlyhas1element(itself).Iassumeyourelookingforthedivsinsideofit.Alsoifyouhavedivsinsideofyourdivs,usethemorespecificchildselector[3]:$(.results>div).eq(index).replaceWith(html);Whichhasthesameeffectasusingthe.childrenmethod[4]withaselectorfilter:$(.results).children(:eq(+index+)).replaceWith(html);Ormoresimply,concatenatingitallinasingleselect...
$(.results div).eq(index).replaceWith(html);Fiddle[1]
You should use .eq()[2] to filter the set of matched elements inside a jQuery object by its index. Also $(.results) only has 1 element (itself). I assume youre looking for the divs inside of it.
Also if you have divs inside of your divs, use the more specific child selector[3]:
$(.results > div).eq(index).replaceWith(html);Which has the same effect as using the .children method[4] with a selector filter:
$(.results).children(:eq( + index + )).replaceWith(html);Or more simply, concatenating it all in a single selector:
$(.results > div:eq( + index + )).replaceWith(html);Fiddle[5]
There are many other ways to get this done with jQuery, but these should be more than enough. =]
References Fiddle (jsfiddle.net) .eq() (api.jquery.com) child selector (api.jquery.com) .children method (api.jquery.com) Fiddle (jsfiddle.net)