-3

Heelo, I know it seems to be a stupid question, but it makes me crazy

I have some buttons in my page

<ul class="nav navbar-nav">
    <li><button type="button" id="start" class="btn btn-default navbar-btn">Start</button></li>
    <li><button type="button" id="pause" class="btn btn-default navbar-btn" disabled="disabled"><span class="glyphicon glyphicon-pause" aria-hidden="true"></span> Pause</button></li>
    <li><button type="button" id="play" class="btn btn-default navbar-btn" disabled="disabled"><span class="glyphicon glyphicon-play" aria-hidden="true"></span> Play</button></li>
    <li><button type="button" id="finish" class="btn btn-default navbar-btn" disabled="disabled"><span class="glyphicon glyphicon-eject" aria-hidden="true"></span> Close</button></li>
</ul>

Now I want to dynamically use jQuery to change one button text; for example I need to change the "Start" text. I tried the following code without success:

$("#start").text = "Open PHASE";

 

j08691
  • 204,283
  • 31
  • 260
  • 272
agodoo
  • 409
  • 6
  • 21

5 Answers5

3

jQuery's .text() is a method. (You can see the documentation here)

This means that to set the text, you have to pass it a parameter, (the text you want to change it to)

$("#start").text("Open PHASE");

If you don't pass it anything, like $("#start").text();, it will return the current text of the element, which you can store in a variable and use later, like such

var text = $("#start").text();
Andrew Brooke
  • 12,073
  • 8
  • 39
  • 55
1

text is a function, not a property. You need to use:

$("#start").text("Open PHASE");

For more info see, read the jQuery documentation.

tbrisker
  • 15,518
  • 1
  • 16
  • 17
0

It's .text() you're after -

$("#start").text("Open PHASE");
John C
  • 3,052
  • 3
  • 34
  • 47
0

You are using it wrong its .text()

$("#start").text("Open PHASE");
r007
  • 305
  • 1
  • 2
  • 17
0

Is this what you're looking for?

$('#start').on('click', function() {
    var txt = $(this).text();
    $(this).text( txt == 'Start' ? 'Open PHASE' : 'Start' );
});

$('#start').on('click', function() {
    var txt = $(this).text();
    $(this).text( txt == 'Start' ? 'Open PHASE' : 'Start' );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul class="nav navbar-nav">
    <li><button type="button" id="start" class="btn btn-default navbar-btn">Start</button></li>
    <li><button type="button" id="pause" class="btn btn-default navbar-btn" disabled="disabled"><span class="glyphicon glyphicon-pause" aria-hidden="true"></span> Pause</button></li>
    <li><button type="button" id="play" class="btn btn-default navbar-btn" disabled="disabled"><span class="glyphicon glyphicon-play" aria-hidden="true"></span> Play</button></li>
    <li><button type="button" id="finish" class="btn btn-default navbar-btn" disabled="disabled"><span class="glyphicon glyphicon-eject" aria-hidden="true"></span> Close</button></li>
</ul>
PeterKA
  • 24,158
  • 5
  • 26
  • 48