0
<div id="nav">
    Some Text
</div>

<input type="text" id="color"><button onClick="changeColor()">Change</button>

<script>

    function changeColor(){

        var colorCode = document.getElementById('color').value;
        var  nav = document.getElementById('nav');

        nav.style.background = "'"+colorCode+"'";
    }

</script>

When i enter for ex.: red in the input field and than press the button, nothing happens.

6 Answers 6

1

You don't need those single quotes, just pass the value itself into the background attribute.

function changeColor(){

        var  nav = document.getElementById('nav');
        nav.style.background = document.querySelector("#color").value;
    }

Sign up to request clarification or add additional context in comments.

2 Comments

Rebeka, Thank you very much.
@klmjiub if it's your answer please vote it and set and answer. thanks.
1

1.please refer to below code which should help you

function changeColor() {
        var colorCode = document.getElementById('color').value;
        var nav = document.getElementById('nav');

        nav.style.background = colorCode;
    }
<div id="nav">
        Some Text
    </div>
    <input type="text" id="color"><button onClick="changeColor()">Change</button>

Comments

0

change
nav.style.background = "'"+colorCode+"'";
to
nav.style.background = colorCode;

Comments

0
<div id="nav">
    Some Text
</div>

<input type="text" id="color"><button onClick="changeColor()">Change</button>

<script>

    function changeColor(){

        var colorCode = document.getElementById('color').value;
        var nav = document.getElementById('nav');

        nav.style.backgroundColor = colorCode;
    }

</script>

Comments

0

Try this this will work:

function changeColor(){

var colorCode = document.getElementById('color').value;
 $("#nav").css('background', colorCode);
   }

Comments

0

Below code will work. Sir check.

function changeColor(){

        var colorCode = document.getElementById('color').value;
       document.getElementById("nav").style.color = '#'+colorCode;
    }
<div id="nav">
    Some Text
</div>

<input type="text" id="color" maxlength ="6"><button onClick="changeColor()">Change</button>

1 Comment

These are the sample color you can test it. You add your desired color also. 323e32 , 254033

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.