Saturday 15 September 2012

javascript - Unable to slice strings retrieved from div's -



javascript - Unable to slice strings retrieved from div's -

i'm having problem slicing text retrieved div javascript/jquery. thought piece every string , .text() function returned string fail see problem. help appreciated!

fiddle

html:

<body> <div class="vaknaam">div 1 :<span class="totaal">55%</span> </div> <div class="vaknaam">div 2 :<span class="totaal">60%</span> </div> <div class="vaknaam">div 3 :<span class="totaal">64%</span> </div> <div class="vaknaam">div 4 :<span class="totaal">76%</span> </div> <div class="vaknaam">div 5 :<span class="totaal">63%</span> </div> </body>

javascript:

$(function () { var divs = {}; var tempstring, vakken = {}; $('.vaknaam').each(function (key, value) { tempstring = $(value).contents().filter(function () { homecoming this.nodetype == 3; }).text(); tempstring = tempstring.slice(0, - 2); vakken[tempstring] = $(value).children('span').text(); }); (var property in vakken) { $('body').append("<p>" + property + "</p>"); } });

you have several errors.

you need declare initialise var vakken. the slice method not modify string should assign returns something. property var index need inquire vakken[property]

your code should this:

$(function () { var divs = {}; var tempstring, vakken = {}; //1 $('.vaknaam').each(function (key, value) { tempstring = $(value).contents().filter(function () { homecoming this.nodetype == 3; }).text(); tempstring = tempstring.slice(0, -4); //2 vakken[tempstring] = $(value).children('span').text(); }); (var property in vakken) { $('body').append("<p>" + property + "</p>"); //3 } });

check out codepen.

javascript jquery string slice

No comments:

Post a Comment