batman007
batman007

Reputation: 31

How to add text to HTML using javascript when there is no id name or particular class is present inside a tag?

I want to add some text to my HTML page in an anchor tag using javascript but there is no id,name,or class present inside that anchor tag. Is there any way to do this. Sample code is given below.

<div>
  <table>
    <tbody>
      <tr>
        <th>
          <span ng-if="smData.standard==smData.stdnr" class="ng-scope"> 
          <a ng-click="m1(a,b)" class="ng-binding" href="/">
              Hello.
          </a> 
        </span>
        </th>
      </tr>
    </tbody>
  </table>
</div>

Now I want to add something to Hello(say world.)

Upvotes: 0

Views: 64

Answers (3)

Jack Bashford
Jack Bashford

Reputation: 44145

Use an attribute selector:

document.querySelector("a[ng-click='m1(a, b)']").innerText += " world.";
<a ng-click="m1(a, b)" class="ng-binding" href="/">Hello</a>

Upvotes: 2

Hadia Khan
Hadia Khan

Reputation: 1

document.querySelector('a').innerHTML = "(say World)";

Upvotes: 0

O.O
O.O

Reputation: 1419

Using selectors to track the HTML elements.

Run code snippet.

function reset() {
  document.querySelector("div Table tr span a").innerText += " World!"
  // $("div Table tr span a").text("Hello World");
}
<!--script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script-->
<button onclick="reset()">Change Text</button>
<div>
  <table>
    <tbody>
      <tr>
        <th>
          <span ng-if="smData.standard==smData.stdnr" class="ng-scope"> 
      <a ng-click="m1(a,b)" class="ng-binding" href="/">
          Hello.
      </a> 
    </span>
        </th>
      </tr>
    </tbody>
  </table>
</div>

Upvotes: 1

Related Questions