How to change bg color of list if its value is xyz onclick | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 3

How to change bg color of list if its value is xyz onclick

I have 5 variables $xyz ,$a ,$b, $c, $d. and echo $a , $b, $c, $d for Selection . now one of them is = $xyz . so how to change bgcolor of that variable on selection if selected variable is = $xyz. also so how to implement it in HTML below is the link of my code https://code.sololearn.com/w9wf4viYsLhT/#php

31st Jan 2018, 9:39 AM
Harish Suthar
Harish Suthar - avatar
31 Antworten
+ 6
@Harish, look at this, it's not great, but I guess it would give you an idea, I haven't been coding web for long, so bear me if it's ugly : ) https://code.sololearn.com/WsC5550LZSUg/?ref=app
31st Jan 2018, 7:20 PM
Ipang
+ 7
Link your code with the question if you want people to help, it's hard to imagine what you're trying to accomplish just by reading the question : )
31st Jan 2018, 9:22 AM
Ipang
+ 6
We need to get a reference to the label for the selected radio button to change its background color, I don't know how to do it, the label is not linked with the radio button, what do you think? got any ideas?
1st Feb 2018, 6:59 AM
Ipang
+ 6
@Harish check the code again, I made some changes...
1st Feb 2018, 8:47 AM
Ipang
+ 6
Well, you can try to use buttons if you want to, it's your choice...
1st Feb 2018, 9:07 AM
Ipang
+ 6
The reason of the submit button is to allow the visitor to change their choice before processing if they changed their mind. You can choose to directly submit the choice when visitor click on one of the choices, without submit button, but it means visitors need to be careful in choosing, because they can't change the choice once the choice is submitted. If you want button in place of radio button you can use multiple submit buttons, once either one is clicked the form is submitted. That's just my idea...
1st Feb 2018, 9:38 AM
Ipang
+ 5
@Harish, multiple choice in the lesson appears to be using radio buttons (cmiiw), the thing that worries me is that if we use JavaScript to change the choices appearance than the visitor can view the source, and can figure out the right answer by checking which choice will change the background color when clicked. I guess this is also the reason why SoloLearn only allows game in the app.
31st Jan 2018, 2:12 PM
Ipang
+ 5
I'd suggest you create a function in php or JS and store it in a diff file and then call it whenever needed.
31st Jan 2018, 5:44 PM
Rugved Modak
Rugved Modak - avatar
+ 5
Yes, just edit CSS for radio buttons' label .rlabel{ display:block; border:#c0c0c0 thin solid; background-color:#abcdef; } I guess it's possible, but it means the visitor cannot change their choice, with the button visitor can still change their choice before submitting if they weren't sure of their choice.
1st Feb 2018, 6:36 AM
Ipang
+ 5
I tried to change background color for the radio button in the JS, but it doesn't work: sel.style.backgroundColor="green"; We need to get a reference to the label for the selected radio button, I'm searching how to do that...
1st Feb 2018, 7:20 AM
Ipang
+ 5
What do you mean every time of true ans? I am not understanding you...?
1st Feb 2018, 9:03 AM
Ipang
+ 3
if $x == $xyz { echo ' <body style="background-color:green">' } This should work
31st Jan 2018, 12:27 PM
Rugved Modak
Rugved Modak - avatar
+ 3
implement my whole ans in HTML, its the same, the echo should do the job there
31st Jan 2018, 12:36 PM
Rugved Modak
Rugved Modak - avatar
+ 3
do you want a button?
31st Jan 2018, 12:48 PM
Rugved Modak
Rugved Modak - avatar
+ 3
sorry I don't have a solid answer to that right now
31st Jan 2018, 1:13 PM
Rugved Modak
Rugved Modak - avatar
+ 2
here is my code an at the end of code read comment for description @Ipang
31st Jan 2018, 9:40 AM
Harish Suthar
Harish Suthar - avatar
+ 2
should I have to do it using radio or select option?
31st Jan 2018, 4:18 PM
Harish Suthar
Harish Suthar - avatar
+ 2
thanks bro@Ipang
1st Feb 2018, 1:17 AM
Harish Suthar
Harish Suthar - avatar
+ 2
I mean after select the option color should change as per right or wrong like if ans is write bgcolor is green and if ans is wrong bgcolor is red
1st Feb 2018, 6:39 AM
Harish Suthar
Harish Suthar - avatar
+ 2
thanks again for helping me
1st Feb 2018, 7:21 AM
Harish Suthar
Harish Suthar - avatar