2

I am trying to get an element using attribute. I want to change the text that says "Card security code" to "CVV". There are many label elements so I can't get element by the tag name.

<label for="cvv">Card security code <span class="required">*</span></label>

1 Answers1

7

You can use attribute value selector to get the , then use descendant selector to get span's afterwards its previousSibling property to target the text node to change its nodeValue.

$('label[for="cvv"] span')[0].previousSibling.nodeValue = 'CVV'
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label for="cvv">Card security code <span class="required">*</span></label>
Satpal
  • 132,252
  • 13
  • 159
  • 168
  • 2
    For those wondering what the `[0]` does, since every jQuery object also masquerades as an array, you can use the dereferencing operator `[0]` to get at the underlying JavaScript node to easily use `previousSibling`. `.get(0)` would also have worked. – j08691 Mar 15 '18 at 13:32