I'm trying to style some HTML, generated from Google Calendar, it's something I don't have control off. It's plain simple HTML.
This is what I have:
<li><a><span class="data">When: gio 27 gen 2011<br /><br />Where: XXXX<br />Event state: confirmed</span></a></li>
<li><a><span class="data">When: gio 20 gen 2011<br /><br />Where: XXXX<br />Event state: confirmed</span></a></li>
This is what I'm trying to get:
<li>
<a>
<span class="when">When:</span>
<span class="day">gio</span>
<span class="number">27</span>
<span class="month">gen</span>
</a>
</li>
<li>
<a>
<span class="when">When:</span>
<span class="day">gio</span>
<span class="number">20</span>
<span class="month">gen</span>
</a>
</li>
With this messy code:
$(function(){
var words = $('div.data').text().split(" ");
$("div.data").html("<span>" + words + "</span>");
$("div.data span:first").addClass("when");
});
And this is what I get:
<li>
<a title="" class="tip">
<div class="data">
<span class="when">
<a xmlns="http://www.w3.org/1999/xhtml">when:,gio,27,gen,2011
where:,XXX,XXX
Event,state:,confirmedWhen:,gio,20,gen,2011
where:,XXX,XXX
Event,sate:,confirmed
</a>
</span>
</div>
</a>
</li>
As you can see I have this new <a> element (why?) and both <li> texts are inside the same <span>. Twice (to be honest, the second doesn't have the "when" class). This should not be hard to do. It's just too hard for me.
split(), and then you glue in the array without doing anything else to it. Thus, Javascript turned the array back into a string by concatenating the strings with commas between them, which is its default behavior.