Javascript: change child style

3881 views javascript
8

I would like to change the color of the <span> tag, when clicking on the containing <td>:

<td onClick='myfunc(this)'>
    <span class='spanClass'>text</span>
</td>

what should the myfunc be like?

answered question

2 Answers

9

Set element.style.color in the function

function myfunc(el){
  el.firstElementChild.style.color = 'red';
}
<table>
  <tr>
    <td onClick='myfunc(this)'>
        <span class='spanClass'>text</span>
    </td>
  </tr>
</table>

posted this
10

try this one $(this).find('.spanclass').css({"color":"red")

or

$(this).find('span').css({"color":"red")

for more details

posted this

Have an answer?

JD

Please login first before posting an answer.