سؤال

My HTML script is

<html>

    <body>
    <b><font color="green">Please select a check box to change the color of text </font> </b><br>
    <input type="checkbox" name="red"  id="chkbx" > &nbsp; Red<br>
    <input type="checkbox" name="green" id="chkbx" value="1"> &nbsp; Green <br>
    <input type="checkbox" name="blue" id="chkbx" value="2"> &nbsp; Blue <br>
    <input type="checkbox" name="orange" id="chkbx" value="3"> &nbsp; Orange <br>
    <input type="checkbox" name="yellow" id="chkbx" value="4"> &nbsp; Yellow <br>
    <p id="demo"></p>

    <button onclick="myfunction()">My Choice</button>
<br><br>
    <h style="color: red; font-weight: bold;"> </h>
    <p id="demo"></p>

i need to get the checkbox value in javascript and if any one checkbox is selected that colour will apply to a text to display on the output. how to do?

هل كانت مفيدة؟

المحلول

First off - don't use the tag as it has been deprecated since 1999 use

<p style='font-weight:bold; color:green'>....</p>

Instead of checkboxs use radio buttons

<input type="radio" name="red" value="red" onClick="myFunction(this.value);"> &nbsp; Red<br>

Repeat the above for all possible selections. Change your function myFunction() to myFunction( value ) and work from there. The information is passed directly to the function so there is no need to go looking for it.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top