check/uncheck
Hallo ik ben nieuw op het gebied van javascript en ben het een en ander aan het testen. Nu zou ik graag het volgende willen. Als ik op een div klik dan moet deze van achtergrondkleur veranderen en er moet een check box aan gevinkt worden. Het eerste deel heb ik al voor elkaar gekregen. Maar hoe kan ik er nu nog voor zorgen dat ook de checkbox wordt aangevinkt als je er op klikt.
Dit is wat ik al heb:
Dit is wat ik al heb:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="language" content="english">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>Nieuwe pagina 2</title>
<style type="text/css">
#test {
width:20px;
height:20px;
padding:4px;
border:1px dashed #f00;
color:#000;
text-align:center;
cursor:pointer;
}
.black {
background-color:#000;
}
.white {
background-color:#fff;
}
</style>
<script type="text/javascript">
function init(){
document.getElementById('test').onclick=function() {
this.className=(this.className=='black')?'white':'black';
}
}
window.addEventListener?
window.addEventListener('load',init,false):
window.attachEvent('onload',init);
</script>
</head>
<body>
<div id="test" class="black" title="click to change color"></div>
</body>
</html>
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="language" content="english">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>Nieuwe pagina 2</title>
<style type="text/css">
#test {
width:20px;
height:20px;
padding:4px;
border:1px dashed #f00;
color:#000;
text-align:center;
cursor:pointer;
}
.black {
background-color:#000;
}
.white {
background-color:#fff;
}
</style>
<script type="text/javascript">
function init(){
document.getElementById('test').onclick=function() {
this.className=(this.className=='black')?'white':'black';
}
}
window.addEventListener?
window.addEventListener('load',init,false):
window.attachEvent('onload',init);
</script>
</head>
<body>
<div id="test" class="black" title="click to change color"></div>
</body>
</html>
Gewijzigd op 27/03/2012 17:16:59 door Peter paul
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!DOCTYPE
...
<script type="text/javascript">
function init(){
document.getElementById('test').onclick=function() {
this.className=(this.className=='black')?'white':'black';
document.getElementById('my_checkbox').checked = (this.className=='black')?'':'checked';
}
}
...
<body>
<div id="test" class="black" title="click to change color"></div>
<input type="checkbox" id="my_checkbox"/>
</body>
</html>
...
<script type="text/javascript">
function init(){
document.getElementById('test').onclick=function() {
this.className=(this.className=='black')?'white':'black';
document.getElementById('my_checkbox').checked = (this.className=='black')?'':'checked';
}
}
...
<body>
<div id="test" class="black" title="click to change color"></div>
<input type="checkbox" id="my_checkbox"/>
</body>
</html>
Gewijzigd op 27/03/2012 18:21:19 door Kris Peeters
bedankt voor je reactie het werkt perfect. Nou heb ik geprobeerd het een aantal keer op de zelfe pagina te laten uit voeren. maar op de manier die ik heb geprobeerd moet ik elke keer een nieuw script maken kan ik ook een universeel script maken. dit is wat ik heb gemaakt.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="language" content="english">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>Nieuwe pagina 2</title>
<style type="text/css">
.black {
width:20px;
height:20px;
padding:4px;
border:1px dashed #f00;
color:#000;
text-align:center;
cursor:pointer;
background-color:#000;
}
.white {
width:20px;
height:20px;
padding:4px;
border:1px dashed #f00;
color:#000;
text-align:center;
cursor:pointer;
background-color:#fff;
}
</style>
<script type="text/javascript">
function init(){
document.getElementById('test').onclick=function() {
this.className=(this.className=='black')?'white':'black';
document.getElementById('my_checkbox').checked = (this.className=='black')?'':'checked';
}
}
window.addEventListener?
window.addEventListener('load',init,false):
window.attachEvent('onload',init);
</script>
<script type="text/javascript">
function init(){
document.getElementById('test1').onclick=function() {
this.className=(this.className=='black')?'white':'black';
document.getElementById('my_checkbox1').checked = (this.className=='black')?'':'checked';
}
}
window.addEventListener?
window.addEventListener('load',init,false):
window.attachEvent('onload',init);
</script>
</head>
<body>
<div id="test" class="black" title="click to change color"></div>
<input type="checkbox" id="my_checkbox"/>
<div id="test1" class="black" title="click to change color"></div>
<input type="checkbox" id="my_checkbox1"/>
</body>
</html>
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="language" content="english">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>Nieuwe pagina 2</title>
<style type="text/css">
.black {
width:20px;
height:20px;
padding:4px;
border:1px dashed #f00;
color:#000;
text-align:center;
cursor:pointer;
background-color:#000;
}
.white {
width:20px;
height:20px;
padding:4px;
border:1px dashed #f00;
color:#000;
text-align:center;
cursor:pointer;
background-color:#fff;
}
</style>
<script type="text/javascript">
function init(){
document.getElementById('test').onclick=function() {
this.className=(this.className=='black')?'white':'black';
document.getElementById('my_checkbox').checked = (this.className=='black')?'':'checked';
}
}
window.addEventListener?
window.addEventListener('load',init,false):
window.attachEvent('onload',init);
</script>
<script type="text/javascript">
function init(){
document.getElementById('test1').onclick=function() {
this.className=(this.className=='black')?'white':'black';
document.getElementById('my_checkbox1').checked = (this.className=='black')?'':'checked';
}
}
window.addEventListener?
window.addEventListener('load',init,false):
window.attachEvent('onload',init);
</script>
</head>
<body>
<div id="test" class="black" title="click to change color"></div>
<input type="checkbox" id="my_checkbox"/>
<div id="test1" class="black" title="click to change color"></div>
<input type="checkbox" id="my_checkbox1"/>
</body>
</html>
*bump*
Met jQuery kan het zo
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="language" content="english">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>Nieuwe pagina 2</title>
<style type="text/css">
.color_box {
width:20px;
height:20px;
padding:4px;
border:1px dashed #f00;
color:#000;
text-align:center;
cursor:pointer;
}
.black {
background-color:#000;
}
.white {
background-color:#fff;
}
</style>
</head>
<body>
<div>
<div class="color_box black" title="click to change color"></div>
<input class="my_checkbox" type="checkbox"/>
1
</div>
<div>
<div class="color_box black" title="click to change color"></div>
<input class="my_checkbox" type="checkbox"/>
2
</div>
<div>
<div class="color_box black" title="click to change color"></div>
<input class="my_checkbox" type="checkbox"/>
3
</div>
<script src=" https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
(function() {
$(".color_box").live("click", function() {
var correspondingCheckbox = $(this).parent().find(".my_checkbox");
var checked = $(correspondingCheckbox).attr('checked');
if(checked) {
$(correspondingCheckbox).removeAttr('checked');
$(this).addClass('black').removeClass('white');
}
else {
$(correspondingCheckbox).attr('checked', 'checked');
$(this).addClass('white').removeClass('black');
}
});
})();
</script>
</body>
</html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="language" content="english">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>Nieuwe pagina 2</title>
<style type="text/css">
.color_box {
width:20px;
height:20px;
padding:4px;
border:1px dashed #f00;
color:#000;
text-align:center;
cursor:pointer;
}
.black {
background-color:#000;
}
.white {
background-color:#fff;
}
</style>
</head>
<body>
<div>
<div class="color_box black" title="click to change color"></div>
<input class="my_checkbox" type="checkbox"/>
1
</div>
<div>
<div class="color_box black" title="click to change color"></div>
<input class="my_checkbox" type="checkbox"/>
2
</div>
<div>
<div class="color_box black" title="click to change color"></div>
<input class="my_checkbox" type="checkbox"/>
3
</div>
<script src=" https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
(function() {
$(".color_box").live("click", function() {
var correspondingCheckbox = $(this).parent().find(".my_checkbox");
var checked = $(correspondingCheckbox).attr('checked');
if(checked) {
$(correspondingCheckbox).removeAttr('checked');
$(this).addClass('black').removeClass('white');
}
else {
$(correspondingCheckbox).attr('checked', 'checked');
$(this).addClass('white').removeClass('black');
}
});
})();
</script>
</body>
</html>




