tags:

views:

59

answers:

2

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"&gt;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.

+1  A: 
$(function(){
    var words = $('div.data').text().split(" ");    
    $("div.data").html("<span class='when'>" + words[0] + "</span>");
    $("div.data").append("<span class='day'>" + words[1] + "</span>");
    //...etc
});
Jason
Too bad it groups list into the same span… <span class="when"><a xmlns="http://www.w3.org/1999/xhtml">Quando:gioQuando:gio</a></span><span class="day">undefined</span> thank for your help Jason.
Daniele
A: 

You can try this:

$('span.data').each(function() {
  var text = $(this).text();
  text = text.substr(0, text.indexOf("Where")); //remove stuff from "Where:"
  var words = text.split(" ");
  var parent = $(this).parent();
  $(this).remove();
  var classes = ["when", "day", "number", "month", "year"];
  for (var i = 0; i < words.length; i++) {
    parent.append($("<span />").addClass(classes[i]).text(words[i]));
  }
});
cambraca
thanks cambraca, this is not working... maybe I can have this string splitted into words and then assign at each word a progressive number. I.e. <div "google">when: 29 gen 2010</div> to <div "google"><span class="date 1">when:</span><span class="date 2">29</span> and so on. For me is just a question to have a dom element to style. Thank you for your help
Daniele
what did you get when you ran this script? I tested it on that html you put and it worked for me..
cambraca
I get this:<ul><li><a title="" class="tip"><span class="when"></span></a></li></ul> from this:<li><a title="" class="tip"><span class="data">Quando: gio 27 gen 2011<br><br>Dove: Guidonia Montecelio<br>Stato evento: confermato</span></a></li></ul>I have just made a test page: http://www.ottimomassimo.it/index.php/bassotto/test/
Daniele
I see, change "Where" to "Dove" in the third line in my script. Then it will work. (I assumed you wanted to delete everything besides the date, if not you can modify this easily enough to include the rest of the data)
cambraca
You are right, it works well on Webkit, but not on firefox… does this makes sense?
Daniele
I was testing in Firefox :-|
cambraca
haha! forget to reactivate javascript after testing :) great Job Cambraca, thank you for your help!
Daniele