0

I'm developing a script that needs to find hidden input values based on specific text within a column of the same row. I do not have control over the HTML.

Example Fiddle

In this working fiddle, we must first find the last column that contains the text 'Year to Date' (last column due to nested tables). Then we use a lot of jQuery to find a hidden input in the same row that contains the value 2.33333 we need.

The Javascript I wrote works but I see a lot of repetition and it seems very inefficient. Is there a way to optimize the determination of this group of variables?

var YTD = $('td:contains("Year to Date")').last().siblings().eq(7).find('input').val();
var EOY = $('td:contains("End-of-Year")').last().siblings().eq(7).find('input').val();
var qtr1 = $('td:contains("Quarter 1")').last().siblings().eq(7).find('input').val();
var qtr2 = $('td:contains("Quarter 2")').last().siblings().eq(7).find('input').val();
var qtr3 = $('td:contains("Quarter 3")').last().siblings().eq(7).find('input').val();
var qtr4 = $('td:contains("Quarter 4")').last().siblings().eq(7).find('input').val();

We cannot count on a pattern of id/name for the related input fields other than they will always occur in eq(7) related to the originating td.

3
  • can you change the html markup Nov 22, 2014 at 2:24
  • Nope (see my first line) - I have no control over the HTML.
    – Paul
    Nov 22, 2014 at 2:24
  • What about Table Header? Nov 22, 2014 at 2:32

1 Answer 1

3

You could extract it to a function, which will help with code reuse:

function findInputFromCellText(cellText) {
    return $('td:contains("' + cellText + '")')
             .last().siblings().eq(7).find("input").val();
}

Then you could do:

var vals = {
  YTD: findInputFromCellText("Year to Date"),
  EOY: findInputFromCellText("End-of-Year"),
  qtr1: findInputFromCellText("Quarter 1")
  /* So on */
};

Since the quarters seem to follow a pattern, you could do:

var vals = {
  YTD: findInputFromCellText("Year to Date"),
  EOY: findInputFromCellText("End-of-Year"),
  quarters: [1, 2, 3, 4].map(function(k) {
    return findInputFromCellText("Quarter " + k);
  })
};

You would then access vals.YTD or vals.quarters[0] etc.

Note that in the last example, vals.quarter[0] would be the first quarter and vals.quarter[3] would be the last quarter.

Now, if you want efficiency, you could easily do this:

var findInputFromCellText = (function() {
  var cache = {};
  return function(cellText) {
    if (cellText in cache) return cache[cellText].val();
    var result = $('td:contains("' + cellText + '")')
      .last().siblings().eq(7).find("input");
    cache[cellText] = result;
    return result.val();
  }
})();
3
  • I like your vals variable. Nov 22, 2014 at 2:41
  • I had actually tried something very similar to your method, but it didn't seem to be more efficient (time/call wise). Maybe I should consider this approach, which IS code reuse efficient?
    – Paul
    Nov 22, 2014 at 2:47
  • @Paul See my edit. You can cache results if you want more speed.
    – soktinpk
    Nov 22, 2014 at 2:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.