Vishnu
Vishnu

Reputation: 2452

Change text of button based on attributes

i have searched stackoverflow but i did not find any answers.

Please Check below html

<button type="button" class="nacnchor" value="1">hello</button>
<button type="button" class="nacnchor" value="2">hello</button>
<button type="button" class="nacnchor" value="3">hello</button>

I can change text of button based on class

$('button.nacnchor').text("REWRITE");

But i want to change the value based on attribute value , is ther anything like $('button.nacnchor,attr(value=1)') :p .

Upvotes: 1

Views: 441

Answers (4)

Neel
Neel

Reputation: 11721

try the below code

$("button[value='1']").text("REWRITE");

Upvotes: 0

Incognito
Incognito

Reputation: 20765

When seeking how to use any given library it's always best to *R*ead *T*he *F*unny *M*anual which has an entire page dedicated to this.

...for example, $("a[rel='nofollow']"), will select <a href="example.html" rel="nofollow">Some text</a> but not <a href="example.html" rel="nofollow foe">Some text.

Attribute values in selector expressions must follow the rules for W3C CSS selectors; in general, that means anything other than a valid identifier should be surrounded by quotation marks.

  • double quotes inside single quotes: $('a[rel="nofollow self"]')
  • single quotes inside double quotes: $("a[rel='nofollow self']")
  • escaped single quotes inside single quotes: $('a[rel=\'nofollow self\']')
  • escaped double quotes inside double quotes: $("a[rel=\"nofollow self\"]")

Upvotes: 1

palaѕн
palaѕн

Reputation: 73896

You can do this:

$("button[value='1']").text("REWRITE");

Upvotes: 1

samjudson
samjudson

Reputation: 56853

Attribute selectors are documented here: http://api.jquery.com/attribute-equals-selector/

$("button.nacnchor[value='1']").text("REWRITE");

Upvotes: 3

Related Questions