I have this following code:
$(".preview").find("#panel").each(function(i, val) {
var x = "" + $(val).html();
$(x).find(".dmPageHeadline").prepend(" 1 - ");
console.log($(x).html());
html += $(x).html();
});
This code iterates through all accordion panels, concatenates its content and add " 1 - " to the header ( class "dmPageHeadline" ).
Probably it's a simple detail, but i cannot figure it out... this line
$(x).find(".dmPageHeadline").prepend(" 1 - ");
doesn't preprend the desired text.
Anyone can point me out what is wrong here?
Thank you very much and happy new year !
Teixeira
In your code you have
#paneland you say you’re looping through them…this shouldn’t be the case though, an ID needs to be unique, otherwise you’ll get all sorts of weird or non-existent behavior.Instead give the elements a
class="panel"instead ofid="panel". As for the jQuery side, you can simplify it down to:Here’s a working example of this in action
If you wanted the index to climb, then do something like this:
The
.prepend()function can take a string, it’s just the way you were prepending that was a little odd 🙂