Search code examples
javascriptsubmit-button

Change submit button value with Javascript - including line break


When I define the value of a submit button in HTML, I can cause a line break:

<script>
    function change_value() {
        if (document.getElementById('click_me').checked) {
            document.getElementById('next').value='Next';
        } else {
            document.getElementById('next').value='Text with&#13;&#10;line break';
        }
    }
</script>

<form>
    <input id="click_me" type="checkbox" name="click_this" value="1" onclick="change_value()">
    <input id="next" type="submit" value="Text with&#13;&#10;line break">
</form>

Before clicking the checkbox, the submit button looks like this:

enter image description here

But when I set that value using JavaScript, the line breaks are being displayed as text:

enter image description here

How can I insert a text with line break using JavaScript?


Note.

This only needs to work in recent versions of Firefox.


Solution

  • In js the line break character is \n, that should be used when you set the value property

    function change_value() {
      if (document.getElementById('click_me').checked) {
        document.getElementById('next').value = 'Next';
      } else {
        document.getElementById('next').value = 'Text with\nline break';
      }
    }
    <input id="click_me" type="checkbox" name="click_this" value="1" onclick="change_value()">
    <input id="next" type="submit" value="Text with&#13;&#10;line break">