I am trying to change the background color using jQuery. What am I doing wrong? I know that this can be done using CSS a lot easier, but I'm trying to do it using jQuery.
Link to jsfiddle . I am trying to change the background "Hello" to yellow.
window.onload=function(){ $('.myClass td').css({'background-color': 'yellow'}); } <table> <tr class="myClass"> <td>Hi</td> </tr> <tr> <td>Bye</td> </tr> </table>
Use document.ready for your JS.
$(document).ready(function(){ $('.myClass td').css({'background-color': 'yellow'}); });
Use $(document).ready() :
$(document).ready()
See jsfiddle for a working example.
window.onload is probably writing something.
window.onload
Try instead
$(function(){ $('.myClass td').css({'background-color': 'yellow'}); });
This is a shorthand for $(document).ready .
$(document).ready
This discusses the difference between onload and ready events.
onload
ready
Bind your function to the jQuery document.ready event:
document.ready
$(document).ready(function () { $('.myClass td').css({'background-color': 'yellow'}); });
Or, more briefly:
$(function () { $('.myClass td').css({'background-color': 'yellow'}); });
window.onload=function(){ $('.myClass td').css("background-color", "yellow"); }();
Just added to (); at the end to call it.
http://jsfiddle.net/p2Uwx/5/ <- updated fiddle
Source: https://habr.com/ru/post/1499240/More articles:Kafka 0.8 manufacturer, using the property "request.required.acks" - message-queueKendo UI, How to get a widget object from a DOM element when using MVVM? - jqueryScreen reader - jquery popup - javascriptOpenGL paints in 16 colors after changing RGBA color - c ++Apache Solr multicast mapping - solrPaginated chronological queries in multiple collections using MongoDB? - node.jspopup for 1 second - iosMongoDB index optimization when using text search in aggregation structure - mongodbRunning another code after viewing a table - iosboost spirit qi on_error pass error_handler struct link - c ++All Articles