0

I'm trying to use Javascript to change the button display text (in this case, "Push Here") using this code:

var button_list = document.getElementsByTagName('button');
var button = button_list[0];

<button class="btn-primary" id="btn-header-example-cs" data-toggle="modal" data-url="" data-options="" style="display: inline-block;">Push Here</button>

However, I can't change it with button.value = "New Text". What am I doing wrong?

4
  • Buttons don't have values, they have content. Commented Jun 3, 2017 at 23:43
  • 4
    More precisely, the have button.textContent = "New Text" Commented Jun 3, 2017 at 23:44
  • you want change text on click or what?? Commented Jun 3, 2017 at 23:48
  • Yup, `.textContent' was exactly what I was looking for. Thank you, guys! Commented Jun 4, 2017 at 20:26

1 Answer 1

-1
 var button = document.getElementsByName('button') 

It needs to set a button name, now I'll show you

<button name="button" class="btn-primary" id="btn-header-example-cs" data-toggle="modal" data-url="" data-options="" style="display: inline-block;">Push Here</button>

I recommend you use a different name

 var button = document.getElementsByName('mybutton_1') 

and html

<button name="mybutton_1" class="btn-primary" id="btn-header-example-cs" data-toggle="modal" data-url="" data-options="" style="display: inline-block;">Push Here</button>

Now you can use it

button.value = "New Text"

look not getElementsByTagName but getElementsByName

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.