0

i have this script working nicely , but i need to figure this out

the script searches the database on the site for franchise icons and replaces the corresponding franchise name with the icons , however there are occasions where a franchise has no icon and i need to make the script return the text , currently it leaves a big box with an "X" in it , with no name or icon.

  var prop, fdb = null, filter_fn;
  filter_fn = function() {return $(this).text() === fdb.name;};

  for(prop in window.franchiseDatabase) {
    if (/^fid_/.test(prop) && prop !== 'fid_0000') {
      fdb = window.franchiseDatabase[prop];
      $('#brief_standings, #livescoring_summary, #recent_draft_picks')
      .find('td').filter(filter_fn)
      .html( '<a href="' + window.baseURLDynamic + '/' + window.year + '/options?L=' + window.league_id + '&F=' + fdb.id + '&O=01">' + '<img src="' + fdb.icon + '"class="franchiseicon" title="' + fdb.name + '" /></a>' );
    }
  }
MShack
  • 642
  • 1
  • 14
  • 33
  • Is `icon` an `html` element ? , or `text` ? Does `td` include both `img` element and `text` ; or , if no `img` element, only `text` ? If possible, can post details of `icon` , `text` to check for ? Thanks – guest271314 Aug 07 '14 at 17:10

2 Answers2

0

I guess you need something like that

$('#image_id').error(function() {
    alert('Replace your <img> with text again !!!');
});

Or you may check this link How to check if image exists with given url?

Which might solve your problem.

Community
  • 1
  • 1
TheMohanAhuja
  • 1,855
  • 2
  • 19
  • 30
0

Try

  var prop, fdb = null, filter_fn;
  // filter_fn = function() {return $(this).text() === fdb.name;};

  for(prop in window.franchiseDatabase) {
    if (/^fid_/.test(prop) && prop !== 'fid_0000') {
      fdb = window.franchiseDatabase[prop];
      $('#brief_standings, #livescoring_summary, #recent_draft_picks')
      .find('td') // .filter(filter_fn)
      .html(function(i, o) {
        return (
                o === fb.name 
                ? '<a href="' + window.baseURLDynamic + '/' 
                  + window.year + '/options?L=' 
                  + window.league_id + '&F=' + fdb.id + '&O=01">' 
                  + '<img src="' + fdb.icon + '"class="franchiseicon" title="' 
                  + fdb.name + '" /></a>' 
                : 
                // do stuff with `text` , original html , new html
                // return original html `o`, 
                // modifiy, return new html, i.e.g., "name not found"
                  o
                );
      });
    }
  };

jsfiddle http://jsfiddle.net/guest271314/8wgujdeb/

See http://api.jquery.com/html/

guest271314
  • 1
  • 15
  • 104
  • 177