javascript - jQuery: Using Multiple Arrays to Match Class Attribute Which Determines ChildNodes HTML? -
i have weekly schedule update manually using static table.
<table> <tr class="live al tv"> <td>october 2</td> <td>12:30 pm</td> <td class="competition"></td> <td>team v team b</td> <td class="field">1</td> </tr> <tr class="be tv"> <td>october 2</td> <td>6 pm</td> <td class="competition"></td> <td>team c v team d</td> <td class="field">2</td> </tr> <tr class="ga tv"> <td>october 3</td> <td>12:30 pm</td> <td class="competition"></td> <td>team d v team a</td> <td class="field">3</td> </tr> <tr class="live de tv"> <td>october 3</td> <td>6 pm</td> <td class="competition"></td> <td>team c v team b</td> <td class="field">4</td> </tr> </table>
i have 2 arrays. first 1 list of classes:
var compclass = new array(); compclass[0] = 'al', compclass[1] = 'be', compclass[2] = 'ga', compclass[3] = 'de';
the second 1 list of competitions:
var competitions = new array(); competitions[0] = 'alpha', competitions[1] = 'beta', competitions[2] = 'gamma', competitions[3] = 'delta';
what i'm trying match compclass
array, respectively, competitions
array. want if <tr>
class matches 1 of compclass
values, inner text of child element, <td class="competition"></td>
, auto-populate using corresponding competitions
value. if doesn't make sense, here's jquery i'm trying use:
jquery(function($){ $(document).ready(function() { var rowclass = $('table tr[class*=" "]'), compcell = $('td.competition'), fieldcell = $('td.field'); function setcompetition() { for(var i=0;i<compclass.length;++i) { $('tr').attr('class',compclass[i]).each(function() { this.children[2].innerhtml = competition[i]; }); } } }); });
i tried using $.map()
couldn't work. i'm novice jquery--let alone js in general. doing wrong? can offer guidance? appreciated.
i suggest using object:
var compclass = { 'al' : 'alpha', 'be' : 'beta', 'ga' : 'gamma', 'de' : 'delta' } var k = object.keys(compclass); $('tr[class]').each(function() { var prop = $.grep(this.classname.split(' '), function(value) { return k.indexof(value) > -1; })[0]; if (prop) this.children[2].innerhtml = compclass[prop]; });
Comments
Post a Comment