Search code examples
javascripthtmldomdom-manipulation

JavaScript Dom Manipulation to remove text


I want to remove ?w=150" class="courseimg"> from the below html code.

<td class="ninja_column_0 ninja_clmn_nm_uploadaheadshotofyou footable-first-visible" style="display: table-cell;">
  <img id="courseimg" src="https://images.unsplash.com/photo-1438761681033-6461ffad8d80?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1350&q=80" softmerge-inner"="" style="width: 260px; left: -1px;" class="courseimg">?w=150" class="courseimg"&gt;
  <h2 class="entry-title" id="title">Jane Doe</h2>
  <div class="course-meta">
    <h4 id="fullname">by person Name</h4>
    <h5><small>Category:</small> Art/Interior design<br><small>Language:</small> Czech</h5>
  </div>
</td>

There are several HTML nodes on the website so I want to remove them from all of them. I've tried using querySelector to select inner HTML and then tried to manipulate it but it's not working. What I'm doing wrong? Maybe type conversion is the issue here?

Js fiddle for this question: jsfiddle

Just remove the extra text along the image ?w=150" class="courseimg"> using javascript.


Solution

  • Try this:

    document.getElementById('courseimg').nextSibling.textContent="";
    

    Or, to remove all of them:

    document.querySelectorAll('.courseimg').forEach(f=>f.nextSibling.textContent="");
    

    Also note that in your examples you have mismatched quotes