-1

User wants to change the color of the text through php .I am new in Php can someone guide me How to do this ? I was trying this through jquery function. Yellow Red Blue

$("select").change(function () {
    var ID = $(this).children(":selected").attr("id");
    $('#selectBox').css('color', ID);
});
james.garriss
  • 12,959
  • 7
  • 83
  • 96
Punit
  • 33
  • 3
  • 12

2 Answers2

3

I'd suggest, in the absence of any HTML in the question:

// binds the change to a select element (_ALL_ select elements):
$("select").change(function () {
    // assigns the value of the selected option to the 'color' variable
    var color = $(this).val()

    /* changes the CSS of the '#selectBox' element, to set the color
       and updates the text of that element to reflect the chosen option/color:
    */
    $('#selectBox').css('color', color).text(color);
// triggers the change event, to trigger the change-handler on page-load/DOMready
}).change();

JS Fiddle demo.

Coupled with the following HTML:

<select name="color" id="color">
    <option value="red">red</option>
    <option value="blue">blue</option>
    <option value="green">green</option>
</select>
<div id="selectBox"></div>

Note that, if you have multiple select elements on the page, you must use a more specific selector to bind the correct event-handling (unless all the select elements are there to update the same CSS of the same element).

References:

David Thomas
  • 249,100
  • 51
  • 377
  • 410
0

You don't have to check which <option> has been selected. Whenever an <option> has been selected your <select> element get the selected <option>s value. So you can just get the value:

$("select").change(function () {
    var color = $(this).val()
    $('#selectBox').css('color', color);
});

jsFiddle


You can't use php for this as you want to modify the DOM element which is client-sided, where php is only server-sided.

nkmol
  • 8,025
  • 3
  • 30
  • 51