0

I'm trying to update the value of a input button to the same name as before, but as uppercase. I struggle with calling the button value within the button value and keep getting errors. This is what I got now:

function updateButton(button) {
    $(button).val(button.val().ToUpperCase())
}

The error I get is:

Uncaught TypeError: button.val is not a function

HTML:

<input type='button' id='btn1' value='test' class='btn' onclick='updateButton(this)'>
<input type='button' id='btn2' value='test' class='btn' onclick='updateButton(this)'>
<input type='button' id='btn3' value='test' class='btn' onclick='updateButton(this)'>

Any idea on how to update the value within itself?

DaveLar
  • 41
  • 1
  • 8
  • 1
    can you also share the html? Also it is [**toUpperCase**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase) – brk Jul 27 '16 at 07:52
  • `$(button).val(button.value.toUpperCase())` or `$(button).val(function(i, v){ return v.toUpperCase(); })` – Pranav C Balan Jul 27 '16 at 07:53
  • Maybe because `button` does not have `value` attribute? – Justinas Jul 27 '16 at 07:53
  • wrap button in $ again `$(button).val($(button).val().toUpperCase())` – eltonkamami Jul 27 '16 at 07:54
  • replace ` button.val().ToUpperCase()` to `button.val.ToUpperCase() ` – Jees K Denny Jul 27 '16 at 07:54
  • First, `button` implies a variable (you may have meant `$('button')`), and second a ` – David Thomas Jul 27 '16 at 07:55

7 Answers7

1

Try:

$(button).val($(button).val().toUpperCase());

the second button needs to be wrapped by jQuery as well, and the toUpperCase starts with (oh the irony) a lower-case t.

ADyson
  • 57,178
  • 14
  • 51
  • 63
0

Firstly note that the method to change case is toUpperCase(), not ToUpperCase().

From the error you're getting it would appear that the button variable contains a DOMElement, not a jQuery object, and hence the error when trying to access jQuery's val() method. Instead, you could use the value property of the DOMElement directly:

function updateButton(button) {
    $(button).val(button.value.toUpperCase())
}

Better still, you could use unobtrusive JavaScript to attach your event handlers:

$('.btn').click(function() {
  $(this).val(this.value.toUpperCase());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='button' id='btn1' value='test' class='btn' />
<input type='button' id='btn2' value='test' class='btn' />
<input type='button' id='btn3' value='test' class='btn' />
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
0

You are mixing jQuery objects with other js variables. $(button) is a jQuery object which selects all elements of type button, while button is simply a variable and if not defined before is of type undefined.

var button = $(button);
function updateButton(button) {
    button.val(button.val().toUpperCase())
}
oshell
  • 8,923
  • 1
  • 29
  • 47
0

You are getting as .val() is jQuery method and you are using it with native DOM element.

You need to create a jQuery object to use .val(), manipulation can be achieved using val(fn) method

$(button).val(function(_, value){
    return value.toUpperCase()
})
Satpal
  • 132,252
  • 13
  • 159
  • 168
0

I'd suggest:

// select all <input> elements with a 'type' attribute
// equal to 'button'; then use the on() method - and
// its anonymous function - to bind the anonymous
// function as the 'click' event-handler:
$('input[type=button]').on('click', function(){

    // set the value of the current element -
    // 'this' will be the clicked input element -
    // to the string-value of its name attribute
    // set to uppercase:
    this.value = this.name.toUpperCase();
});
David Thomas
  • 249,100
  • 51
  • 377
  • 410
-1

$('#button_id').val(button.val().ToUpperCase())

-1

Try this:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
        function updateButton(button) {
          $(button).val($(button).val().toUpperCase());
        }
    </script>
    <input type="button" value="hello" onclick="updateButton(this)">
hmak.me
  • 3,770
  • 1
  • 20
  • 33