最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Pass div to javascript function and change its style - Stack Overflow

programmeradmin2浏览0评论

I'm trying to pass a div's id to a javascript function that does something simple, like change the background color of the div.

Weird thing is, my code works in the w3schools Tryit editor, but not in JSFiddle. It also doesn't work in my piler (Coda 2).

Is this even the right way of going about this? Here's my code:

<!DOCTYPE html>
<html>
<head>
<script>
function changeBackgroundColor(asdf)
{
    asdf.style.backgroundColor = "#fff000";
}
</script>
</head>
<body>

<div id="myDiv" style="background:red"> this is my div </div>
<button onclick="changeBackgroundColor(myDiv)"> Set background color </button>

</body>
</html>

and here's the JSFiddle stuff: /

I'm trying to pass a div's id to a javascript function that does something simple, like change the background color of the div.

Weird thing is, my code works in the w3schools. Tryit editor, but not in JSFiddle. It also doesn't work in my piler (Coda 2).

Is this even the right way of going about this? Here's my code:

<!DOCTYPE html>
<html>
<head>
<script>
function changeBackgroundColor(asdf)
{
    asdf.style.backgroundColor = "#fff000";
}
</script>
</head>
<body>

<div id="myDiv" style="background:red"> this is my div </div>
<button onclick="changeBackgroundColor(myDiv)"> Set background color </button>

</body>
</html>

and here's the JSFiddle stuff: http://jsfiddle/BH9Rs/

Share Improve this question asked Oct 17, 2013 at 19:16 ryantuckryantuck 6,67411 gold badges64 silver badges73 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 6

You need to use document.getElementById to get the element from the id

function changeBackgroundColor(asdf){
    document.getElementById(asdf).style.backgroundColor = "#fff000";
}

and pass the id like this (in single quotes)

<button onclick="changeBackgroundColor('myDiv')"> 

And in your fiddle put your function inside the Head section. (select no-wrap in Head) at left side Framework and Extension option

Js Fiddle Demo

Change the script placement bo to no wrap - in <head> (second bo box on the left panel)

myDiv is an unknown identifier.

Use document.getElementById() in order to refer to your div:

<button onclick="changeBackgroundColor(document.getElementById('myDiv'))"> Set background color </button>

In addition, you have to move your JS code in your HTML due to the jsFiddle environment: http://jsfiddle/BH9Rs/1/

<script>
    function changeBackgroundColor(asdf) {
        asdf.style.backgroundColor = "#fff000";
    }
</script>
<div id="myDiv" style="background:red">this is my div</div>
<button onclick="changeBackgroundColor(document.getElementById('myDiv'))">Set background color</button>

Inline styles and inline click events? boo.

<div id="myDiv"> this is my div </div>
<button id="clickMe"> Set background color </button>

<script>
    document.getElementById('clickMe').onclick = function(){
        document.getElementById('myDiv').style.backgroundColor = '#fff000';
    }
</script>

You mast do so:

    <!DOCTYPE html>
<html>
<head>
<script>
function changeBackgroundColor(asdf)
{
    document.getElementById(asdf).style.backgroundColor = "#fff000";
}
</script>
</head>
<body>

<div id="myDiv" style="background:red"> this is my div </div>
<button onclick="changeBackgroundColor('myDiv')"> Set background color </button>

</body>
</html>
发布评论

评论列表(0)

  1. 暂无评论