javascript - How to Modify Content in a Text Block -


i have html looks this:

<td class="targettd">   <a href="http://foo.com">     <span>content</span>   </a>   **text want modify**   <span>more content</span> </td> 

targettd iterates dynamic number of times depending on content being rendered. each iteration, need remove substring of ": " beginning of code in text block. unfortunately it's not in own element , don't have ability wrap in neat , tidy id/class. did searching , found js thought might trick:

<script> var mystring = $('.targettd').html(); mystring = mystring.replace(': ',''); $('.targettd').html(mystring); </script> 

but spits out console error:

unable property 'replace' of undefined or null reference.

final update (solution)

thanks @vaibhav coming fix! script did trick:

<script type="text/javascript">      $(function() {         $('.targettd').each(function () {             $(this).html($(this).html().replace(/\:\s/g, ''));         });     });  </script> 

update 1

thanks @benm able stop error producing using following code:

<script> $(function() {     $('.targettd').html(function() { $(this).html().replace(': ', ''); }); }); </script> 

although don't error message, still isn't removing ": " text block. ideas on why might be?

update 2

to provide sharepoint specifics broadly, in response comment @elvislikebear:

  • the specific task i'm trying accomplish hiding column name grouped list. in sp2010 easy hiding ms-gb class. in 2013, microsoft has lumped in expand/collapse button class hiding wholesale not option. i've hid span column name (in code above, span wrapped in a), ": " unhelpfully still in text block, , it's floating there @ beginning of each category name.

  • the code being deployed @ page level snippet, using script editor web part.

brendan, issue code replacing ':' '' not assigning same td again.

replace(':','') function replaces first occurance of ':'.please follow code. replace ':' ''.

with reference "targettd iterates dynamic number of times depending on content being rendered"

i assume need foreach loop iterate on tds.

html :-

<table>     <tr>         <td class="targettd">             <a href="http://foo.com">                 <span>content</span>             </a>             test:test1             <span>more content</span>         </td>     </tr>     <tr>         <td class="targettd">             <a href="http://foo.com">                 <span>content</span>             </a>             test:test1:test2             <span>more content</span>         </td>     </tr>     <tr>         <td class="targettd">             <a href="http://foo.com">                 <span>content</span>             </a>             test:test1:test3:test4             <span>more content</span>         </td>     </tr> </table> 

jquery :-

<script type="text/javascript">      $(function() {         $('.targettd').each(function () {             $(this).html($(this).html().replace(/\:/g, ''));         });     });     </script> 

script tested , works fine.


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -