-1

Throughout the DOM I have values (innerHTML) that I want to replace.

For example

<div id="id1">N/A</div>
...
...
<div id="id2">N/A</div>
...
...
<div id="id3">n/a</div>
...
...

and I want to substitute if with a 0.

How do I do this with a single jQuery call?

CLARIFYING QUESTION I want to select those elements that have N/A or n/a as values (innerHTML), regardless of if they are in a div, a span, a td...

Amarundo
  • 2,357
  • 15
  • 50
  • 68

2 Answers2

0

If all of the replacements you want to do are for div elements you can try this:

$("div").html('0');

You didn't specify any constraints so I based my answer on what you posted.

-1

You just need to use an attribute selector:

$('[id^="id"]').text('0');

If you only want the ones with "N/A" in the text, you would need to loop over all of the divs and check for the "N/A" text.

$('[id^="id"]').each(function(){
   var $t=$(this), txt =$t.text();
   if (txt.toLowerCase() === 'n/a') { //convert to lowercase to match regardless of capitalization
     $t.text('0');
   }
});
Gary Storey
  • 1,779
  • 2
  • 14
  • 19