I am working on javascript based functionality for showing user emotion through smiley for 7 days. 1 Smiley for 1 day.
I did this for user 1 for day 1 by javascript if else. But this is not a good way for doing this task. Because here we have 7 days and as per enter value in text field it will show smiley.
Somehow I feel array will be good option. But I don't know how to achieve this task through by array.
What I am thinking first I have to call 7 smiley background in array, then match them as per user entered value. But the problem is for user2 and user3. I think I have to twice time duplicate user1 code for user2 and user3.
Anyone can suggest some good solution.
function checkvalue()
{
var iuser1 = document.getElementById("user1").value;
var iuser2 = document.getElementById("user2").value;
var iuser3 = document.getElementById("user3").value;
if ( iuser1 == 1 )
{
document.getElementById("c1day1").style.backgroundImage = "url('https://cdn2.iconfinder.com/data/icons/very-emotional-emoji/64/18_EmoticonsHDcom.png')";
}
else if ( iuser1 == 2 )
{
document.getElementById("c1day1").style.backgroundImage = "url('https://cdn2.iconfinder.com/data/icons/very-emotional-emoji/64/53_EmoticonsHDcom.png')";
}
else if ( iuser1 == 3 )
{
document.getElementById("c1day1").style.backgroundImage = "url('https://cdn2.iconfinder.com/data/icons/very-emotional-emoji/64/69_EmoticonsHDcom.png')";
}
else if ( iuser1 == 4 )
{
document.getElementById("c1day1").style.backgroundImage = "url('https://cdn2.iconfinder.com/data/icons/very-emotional-emoji/64/42_EmoticonsHDcom.png')";
}
else if ( iuser1 == 5 )
{
document.getElementById("c1day1").style.backgroundImage = "url('https://cdn2.iconfinder.com/data/icons/very-emotional-emoji/64/72_EmoticonsHDcom.png')";
}
else if ( iuser1 == 6 )
{
document.getElementById("c1day1").style.backgroundImage = "url('https://cdn2.iconfinder.com/data/icons/very-emotional-emoji/64/26_EmoticonsHDcom.png')";
}
else if ( iuser1 == 7 )
{
document.getElementById("c1day1").style.backgroundImage = "url('https://cdn2.iconfinder.com/data/icons/very-emotional-emoji/64/40_EmoticonsHDcom.png')";
}
}
input[type="text"]{
display:block;
width:100%;
margin-bottom:10px;
}
.icon-bg{
width:64px;
height:64px;
display:block;
background-position:center;
}
<h5> Check result by javascript code </h5>
<input type="text" name="in" id="user1" placeholder="Put value for user 1 between 1-7">
<input type="text" name="in" id="user2" placeholder="Put value for user 2 between 1-7">
<input type="text" name="in" id="user3" placeholder="Put value for user 3 between 1-7">
<input type="submit" id="check" value="Check" onclick="checkvalue()">
<hr>
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr>
<td> <strong> </strong> </td>
<td> </td>
<td> <strong> Day 1 </strong> </td>
<td> <strong> Day 2 </strong> </td>
<td> <strong> Day 3 </strong> </td>
<td> <strong> Day 4 </strong> </td>
<td> <strong> Day 5 </strong> </td>
<td> <strong> Day 6 </strong> </td>
<td> <strong> Day 7 </strong> </td>
</tr>
<tr>
<td> <strong> User 1 </strong> </td>
<td> </td>
<td> <div class="icon-bg" id="c1day1"> </div> </td>
<td> <div class="icon-bg" id="c1day2"> </div> </td>
<td> <div class="icon-bg" id="c1day3"> </div> </td>
<td> <div class="icon-bg" id="c1day4"> </div> </td>
<td> <div class="icon-bg" id="c1day5"> </div> </td>
<td> <div class="icon-bg" id="c1day6"> </div> </td>
<td> <div class="icon-bg" id="c1day7"> </div> </td>
</tr>
<tr>
<td> <strong> User 2 </strong> </td>
<td> </td>
<td> <img src="https://cdn2.iconfinder.com/data/icons/very-emotional-emoji/64/01_EmoticonsHDcom.png" alt="icon" width="48px" height="48px"> </td>
<td> <img src="https://cdn2.iconfinder.com/data/icons/very-emotional-emoji/64/72_EmoticonsHDcom.png" alt="icon" width="48px" height="48px"> </td>
<td> <img src="https://cdn2.iconfinder.com/data/icons/very-emotional-emoji/64/22_EmoticonsHDcom.png" alt="icon" width="48px" height="48px"> </td>
<td> <img src="https://cdn2.iconfinder.com/data/icons/very-emotional-emoji/64/59_EmoticonsHDcom.png" alt="icon" width="48px" height="48px"> </td>
<td> <img src="https://cdn2.iconfinder.com/data/icons/very-emotional-emoji/64/70_EmoticonsHDcom.png" alt="icon" width="48px" height="48px"> </td>
<td> <img src="https://cdn2.iconfinder.com/data/icons/very-emotional-emoji/64/71_EmoticonsHDcom.png" alt="icon" width="48px" height="48px"> </td>
<td> <img src="https://cdn2.iconfinder.com/data/icons/very-emotional-emoji/64/40_EmoticonsHDcom.png" alt="icon" width="48px" height="48px"> </td>
</tr>
<tr>
<td> <strong> User 3 </strong> </td>
<td> </td>
<td> <img src="https://cdn2.iconfinder.com/data/icons/very-emotional-emoji/64/50_EmoticonsHDcom.png" alt="icon" width="48px" height="48px"> </td>
<td> <img src="https://cdn2.iconfinder.com/data/icons/very-emotional-emoji/64/51_EmoticonsHDcom.png" alt="icon" width="48px" height="48px"> </td>
<td> <img src="https://cdn2.iconfinder.com/data/icons/very-emotional-emoji/64/53_EmoticonsHDcom.png" alt="icon" width="48px" height="48px"> </td>
<td> <img src="https://cdn2.iconfinder.com/data/icons/very-emotional-emoji/64/20_EmoticonsHDcom.png" alt="icon" width="48px" height="48px"> </td>
<td> <img src="https://cdn2.iconfinder.com/data/icons/very-emotional-emoji/64/19_EmoticonsHDcom.png" alt="icon" width="48px" height="48px"> </td>
<td> <img src="https://cdn2.iconfinder.com/data/icons/very-emotional-emoji/64/37_EmoticonsHDcom.png" alt="icon" width="48px" height="48px"> </td>
<td> <img src="https://cdn2.iconfinder.com/data/icons/very-emotional-emoji/64/69_EmoticonsHDcom.png" alt="icon" width="48px" height="48px"> </td>
</tr>
</tbody>
</table>
else ifconstructions, replace it withswitch. Nicer more readable (cleaner) code.