Replace empty value in <div> tag with 0 and wrap it with <span> tag + class name in jQuery
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/12572661/insert-a-span-tag-with-content-in-an-empty-div-tag-in-jquery">Insert a &lt;span&gt; tag with content in an empty &lt;div&gt; tag in jQuery</a>  </p>
</blockquote>



<p>I posted the same question earlier today with 4 answers all seem to be working in jsFiddle but not on my real site. I want to address the issue in a different way so someone might be able to help me out here as I couldn't figure out what was wrong with my code.</p>

<p>Here is the original HTML which I have no control over:</p>

<pre><code>&lt;div class="a"&gt;
   &lt;span class="b"&gt;4&lt;/span&gt;
&lt;/div&gt;
</code></pre>

<p>In the original code, whenever there is no value between the span tags, the entire span line is gone. It leaves the code like this:</p>

<pre><code>&lt;div class="a"&gt; &lt;/div&gt;
</code></pre>

<p>I want to achieve the following result to have the span line appear also even there is no value. </p>

<pre><code>&lt;div class="a"&gt;
   &lt;span class="b"&gt;0&lt;/span&gt;
&lt;/div&gt;
</code></pre>

<p>It seems to me there is a white space between the div tags, so I used a trim method. The 4 answers I got were very similar to mine but nothing worked on my real site. Could it be something else I didn't figure out?</p>

<p>This is what I have tried:</p>

<pre><code>&lt;script&gt;
    jQuery(function ($) {

        if ($.trim($('.a').text()).length==0){
            $(".a").html('&lt;span class="b"&gt;0&lt;/span&gt;');


        };
    });
&lt;/script&gt;
</code></pre>
