You pass the โlabelโ as the id parameter of your changeColor handler, but there is no element with this identifier in the provided HTML. You will need to add several identifiers to your tags and pass them to the onclick handler. For instance:
<label for="model" id="label1">Male</label> <input type="text" name="cars" id="model" /> <input type="button" value="Change Label Color" onclick="return changeColor('label1', 'red')" />
An alternative would be to pass the identifier of the input element, since they already have the identifiers assigned to them. Then you need to change your changeColor handler as follows:
function changeColor(inputId, newColor) { $("#" + inputId).prev().css("color", newColor); }
Edit: Here is jsFiddle demonstrating my second example.
source share