Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing button text with JavaScript doesn't work in Opera (11.11) for <input type="submit" /> elements. Why?

Why is that changing a button text doesn't work in Opera 11.11 for elements like

<input type="submit" value="Asdasd" id="blahblah_button" />

? (Didn't try it in earlier versions yet.)

I tried it with jQuery and with "pure" JavaScript too, none of them worked.
This is the jQuery code I tried:

$('#blahblah_button').val('Blah-blah');

and this is the "pure" JS-code:

document.getElementById('blahblah_button').value = 'Blah-blah';

Why is that none of them worked in Opera 11.11?
It DOES work in IE, Chrome and FF, it surprises me that it doesn't work in Opera.

I have to mention that it DOES work for button tags like this in Opera too:

<button id="test_button" onclick="$(this).text('Blahblah');">Some text</button> 

Thanks for your answers in advance!


EDIT I. (0:40)

I forgot to mention that querying the button's value after the modification gives the result that it seems to work fine, which means it changes the structure in JS DOM, but doesn't rerender the visible button appropriately.

This is the example code with which you can try this behaviour:

http://jsbin.com/inuxix/1/edit

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="hu" xml:lang="hu">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <title>Changing button text</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" type="text/javascript"></script>
    </head>
    <body>
        <p>Button tag - WORKING
            <button onclick="$(this).text('Blahblah_1');" id="test_button">Button_text (button_tag)</button>
        </p>
        <p>Input tag (type: submit) - NOT working
            <input onclick="$(this).val('Blahblah_2');" type="submit" value="Submit_text" id="blahblah_submit_type" />
        </p>
        <p>Input tag (type: button) - WORKING
            <input onclick="$(this).val('Blahblah_3');" type="button" value="Button_text" id="blahblah_button_type" />
        </p>
        <p>
            <button onclick="alert($('#blahblah_submit_type').val());" id="val_button">Getting blahblah_submit_type's value</button>
        </p>
    </body>
</html>

EDIT II. (4:41)

But I also have to mention that it DOES work for input elements with "button" type - so I complemented my code above with an element like this. I also marked which types do and which don't work.


EDIT III.

In the meantime, I tested it, and it doesn't work in Opera <= 11.11, but this bug has been fixed in Opera 11.50 though.

like image 422
Sk8erPeter Avatar asked May 21 '11 22:05

Sk8erPeter


People also ask

How to change button text using JavaScript?

To change button text using JavaScript, the simplest way is to use the textContentproperty along with the getElementByIdmethod. document.getElementById("button").textContent="Submit"; You can also use the innerHTMLproperty to change button text. document.getElementById("button").innerHTML = "Submit"; Let’s say I have the following HTML form:

How to change the text of a button declared with <input type>?

Button Declared with <input type="button">. To change the text of a button that has been declared with <input type="button">tag: use the button's valueproperty. For example:#N# . <input type="button" value="Button Text" id="myButton1"></input> . To change the text of that button, we can use this code:#N# .

How do I replace the text of a button element?

To call this function, pass the ID of the button element (buttonID) and the text to put into the button. Example: replaceButtonText('myButton1', 'new button text'); That will replace the text of the button with the id "myButton" with "new button text".


2 Answers

This is a bug in Opera. I'm not sure what causes it, but a simple test page that renames the button does not trigger any issues, however on @Darin's jsfiddle.net example the bug does appear.

It appears to be a redraw bug. I noticed the width of the button changes to match the new label, but the actual label does not change. Also, shifting away from the page and going back, shows the new label instead of the old one.

I did a quick google and could not find anyone else who's come across it.

Here is a workaround that I found:

$('#blahblah_button').val('there');
$('#blahblah_button').get(0).outerHTML = $('#blahblah_button').get(0).outerHTML;

Perhaps someone can find a cleaner workaround, ideally one that's built into jQuery's val() method. But the best solution is obviously for Opera to fix the bug. You should send them an email about it.

like image 192
Abhi Beckert Avatar answered Sep 21 '22 00:09

Abhi Beckert


@Abhi Beckert, actually some guy found this bug - http://webinterfacetricks.com/opera_submit_bug/ I've tested it and it works:

<html>
<input type="submit" value="A" id="blahblah" onClick="click_input(this)" />
<script>
    function click_input(input)
    {
        input.value = "test";
        input.type = "submit";
    }
</script>
</html>

Yes, it's pretty odd but I hope Opera guys will fix it soon. Can you report them, btw?

like image 27
Vasiliy Ermolovich Avatar answered Sep 22 '22 00:09

Vasiliy Ermolovich