1

Here the code:

 <script type="text/javascript">
 function ChangeColor1()
 {
 t1.style.backgroundColor = 'pink';
 t2.style.backgroundColor = '';
 t3.style.backgroundColor = '';
 }
 function ChangeColor2()
 {
 t1.style.backgroundColor = ''; 
 t2.style.backgroundColor = 'pink';
 t3.style.backgroundColor = '';
 }
 function ChangeColor3()
 {
 t1.style.backgroundColor = '';
 t2.style.backgroundColor = '';
 t3.style.backgroundColor = 'pink';
 }
 </script>
 </head>
 <body>
 <table width="50%" border="1" >
 <tr id="t1" onclick="ChangeColor1();">
 <td>1</td>
 <td>2</td>
 <td>3</td>
 </tr>
 <tr id="t2" onclick="ChangeColor2();">
 <td>4</td>
 <td>5</td>
 <td>6</td>
 </tr>
 <tr id="t3" onclick="ChangeColor3();">
 <td>7</td>
 <td>8</td>
 <td>9</td>
 </tr>
 </table>
 </body>

In this program there are 3 functions are used. I want to do this task using a single function instead of one.

Aelios
12.2k2 gold badges38 silver badges54 bronze badges
asked Sep 13, 2012 at 14:31

2 Answers 2

2
function changeColor(n){
 t1.style.backgroundColor = n == 1 ? 'pink' : '';
 t2.style.backgroundColor = n == 2 ? 'pink' : '';
 t3.style.backgroundColor = n == 3 ? 'pink' : '';
}
... onclick="changeColor(1)" ...

Would be how I'd refactor it. Or better yet make an array of var ts = [t1,t2,t3] then reference ts[n].

var ts = [t1,t2,t3];
function changeColor(n){
 for (var i = 0; i < ts.length; i++){
 ts[i].style.backgroundColor = (i+1) == n ? 'pink' : '';
 }
}

Or you can reference the id directly:

function changeColor(n){
 for (var i = 1; i < 4; i++){
 document.getElementById('t'+i).style.backgroundColor = n == i ? 'pink' : '';
 }
}

You can also take it a step farther and reference the row itself instead of specifying the index as an argument (keeping it generic):

function changeColor(t){
 for (var n = 1; n < 4; n++){
 var tn = document.getElementById('t'+n);
 tn.style.backgroundColor = tn.id == t.id ? 'pink' : '';
 }
}
... onclick="changeColor(this);" ...
answered Sep 13, 2012 at 14:34
Sign up to request clarification or add additional context in comments.

1 Comment

@mplungjan: Coincidental, not intentional. But I can remove them if you'd prefer. I posted my initial solution then saw a re-format on the question and saw IDs in the <tr> and made additional changes. At first pass I hadn't caught the IDs in the code but then syntax highlighting made it more visible.
1

DEMO

NOTE: Not all browsers will accept t1.style without the document.getElementById

function ChangeColor(row) {
 var idx=row.id;
 for (var i=1;i<=3;i++) {
 document.getElementById("t"+i).style.backgroundColor = (idx==="t"+i)?"pink":""; 
 }
}

using

<tr id="t1" onclick="ChangeColor(this);">
<tr id="t2" onclick="ChangeColor(this);">
<tr id="t3" onclick="ChangeColor(this);">
answered Sep 13, 2012 at 14:34

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.