Javascript change color of text and background to input value

前端 未结 3 944
慢半拍i
慢半拍i 2020-12-03 15:38

I\'m going to use javascript to make a function for changing color of background, as well as text simultaneously - based on the value of a text input. I\'ve got the backgrou

3条回答
  •  难免孤独
    2020-12-03 16:30

    Depending on which event you actually want to use (textbox change, or button click), you can try this:

    HTML:

    
    
    This text should have the same color as you put in the text box

    JS:

    function changeBackground(obj) {
        document.getElementById("coltext").style.color = obj.value;
    }
    

    DEMO: http://jsfiddle.net/6pLUh/

    One minor problem with the button was that it was a submit button, in a form. When clicked, that submits the form (which ends up just reloading the page) and any changes from JavaScript are reset. Just using the onchange allows you to change the color based on the input.

提交回复
热议问题