The HTML I have is something like the following:
<div id="container">
<ul>
<li>
<div class="oneClass">
<img src="http:image.jpg">
</div>
<div class="anotherClass" id="oneID">
<div class="twoClass" id="randomID">
<cite class="user">
<a href="http://someURL" >Name</a>
</cite>
<span class="icon user"></span>
<span class="threeClass">
<a href="http:URL#oneID">some text</a>
</span>
</div>
<p class="content" id="randomID">some words content</p>
</div>
</li>
<li>
<div class="oneClass">
<img src="http:image.jpg">
</div>
<div class="anotherClass" id="anotherID">
<div class="twoClass" id="randomID">
<cite class="user">
<a href="http://someURL" >Name</a>
</cite>
<span class="icon user"></span>
<span class="threeClass">
<a href="http:URL#anotherID">some text</a>
</span>
</div>
<p class="content" id="randomID">some words content</p>
</div>
</li>
</ul>
</div>
What I need is to find a way to detect the next ID after oneClass and use to create another div around oneClass.
Example:
<div id="container">
<ul>
<li>
<div id="myName_oneID">
<div class="oneClass">
<img src="http:image.jpg">
</div>
</div>
<div class="anotherClass" id="oneID">
<div class="twoClass" id="randomID">
<cite class="user">
<a href="http://someURL" >Name</a>
</cite>
<span class="threeClass">
<a href="http:URL#oneID">some text</a>
</span>
</div>
<p class="content" id="randomID">
some words content
</p>
</div>
</li>
<li>
<div id="myName_anotherID">
<div class="oneClass">
<img src="http:image.jpg">
</div>
</div>
<div class="anotherClass" id="anotherID">
<div class="twoClass" id="randomID">
<cite class="user">
<a href="http://someURL" >Name</a>
</cite>
<span class="threeClass">
<a href="http:URL#anotherID">some text</a>
</span>
</div>
<p class="content" id="randomID">
some words content
</p>
</div>
</li>
</ul>
</div>
I’ve tried to get it from the anchor like this:
$(".threeClass a").map(function () {
var myID ="myName_" + this.hash.replace(/#/, '');
//alert(myID);
$(".oneClass").each(function () {
$(this).wrap("<div id='" + myID + "' />");
});
});
If I test the alert(myID), it detects correctly but when I try use it to wrap a new div, it creates it with the same Id for all oneClass.
Can it be done?
I’m a newbie at javascript or Jquery and appreciatte the help or guidance on the right way.
Thank you.
Try this
DEMO (See the source).