user379888
user379888

Reputation:

Get element by attribute in jQuery

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>

Upvotes: 2

Views: 126

Answers (1)

Satpal
Satpal

Reputation: 133403

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>

Upvotes: 7

Related Questions