You code does not follow the right principles.
$(function(){ var TURN_X = "X", TURN_O = "O", turn = TURN_O, $listCells = $("td"); function marked() { // define event handler var $this = $(this), tileNum = $this.attr("id"); if ( !($this.hasClass("marked") ) { $this.addClass("marked").text(turn); turn = (turn == TURN_X) ? TURN_O : TURN_X; } } $listCells.click(marked); // attach event handler });
- Wrap everything in the document.ready function. Avoid global variables wherever possible.
- Make use of the fact that jQuery manages
this
for you.this
will always be what you expect if you pass callback functions directly instead of calling them yourself.
Related Posts:
- jQuery $(this) keyword
- jQuery $(this) keyword
- $(document).on(“click”… not working?
- How to set time delay in javascript
- Disable button in jQuery
- JavaScript: filter() for Objects
- Is jQuery $.browser Deprecated?
- jQuery date formatting
- $ versus jQuery
- How to center image in carousel
- How to get a value of an element by name instead of ID
- jquery clear input default value
- How to manage ajax calls and JSON in wordpress
- Display Media Uploader in Own Plugin on WordPress 3.5
- How to add a ” waiting” icon for an ajax in WP frontend?
- Make jQuery library load before plugin files
- What is the simplest ajax upload plugin or script to be used with wordpress?
- Add tinymce buttons to caption textarea in media uploader
- RequireJS in custom theme. Working Example
- How to handle multiple instance of “send_to_editor” js function
- call shortcode in javascript
- inject js after redirect
- Stylizing external SVG files with jQuery and CSS
- wp_enqueuescript won’t load in footer even with true value set?
- jQuery Ajax() doesn’t work when the page is accessed as a WordPress template page
- WordPress REST endpoint not able to reach with jQuery
- modifying a template and adding jQuery to it
- Is placing stuff in page text edit window bad form?
- Add colorpicker to featured image
- How to force jQuery to load as a first script in header in frontend?
- jQuery breaking my wordpress site
- wp_verify_nonce not working
- Passing JSON-encoded HTML from WordPress to JavaScript
- Tags in WordPress 3.2
- php ajax problem – weird 301 responses!
- Populating autocomplete field with custom post type
- Not sure why wp_ajax isn’t working?
- Load created php file data via ajax
- Is it necessary to enqueue jquery?
- Add a class to posts in increments of 2
- Move jQuery to the bottom of the page whilst keeping the WordPress jQuery
- How I got jQuery Quicksand working with WordPress?
- Displaying div from custom-post-type in Fancy Box
- NextGen plugin check failing, why?
- Return an array from get_terms and store as JavaScript array for search autocomplete
- On screen music/audio player that grabs track URL [closed]
- WP http XML response HTML encoding and image display problems
- Using WP_Query to grab custom meta values, foreach to json object
- Do we have to santise html passing into Javascript ? How?
- WP UI style within shortcode – Any Suggestions?
- jShowOff not showing numeric links, shows ‘undefined’ instead
- Javascript stopped working [closed]
- Firebug says jQuery is loaded but $() and jQuery() are not defined
- Why load JS through the CMS framework? [duplicate]
- Why should I deregister jquery?
- Prevent other versions of jquery from loading on static front page
- How to add custon UI jquery & Fancybox script to WP
- Adding URL of PDF from Thickbox in a Meta Box
- jQuery will not work on page from snippet or file
- Jquery not loaded by default in wordpress 5.6
- problem with jquery 3.5.1 in footer
- How to keep HTML form field that is conditional hidden with javasript hidden after page reload?
- Accessing javascript in multiple files [closed]
- Issues loading Jquery on WordPress website
- Script not loading when depending on jQuery
- masonry only works if jquery is called twice
- No Object Properties sending form through AJAX
- Redirect wp_dropdown_pages() to an AJax request?
- how to use wp_editor() here am getting empty result
- jQuery is not defined, working on local server but not online
- Parse wordpress blog data using json api and ajax
- JQuery Issues With WordPress Theme Interface
- problems with jquery external rss feed integration
- wp_enqueue_script calling jQuery at different times for different browsers?
- AJAX calendar navigation returns -1
- A carousel slideshow plugin in JavaScript
- Axis guidelines over an image
- Retrive username of the loged in user using jquery
- How to setup multi-page using jquery?
- $ not defined using jQuery in WordPress
- Get with jQuery the value of an ACF field
- I want to run Sql Query in script how can i do this
- error: n.imagesLoaded is not a function
- WordPress forbidden 403 on delete in production vs local environment
- How to change event in slider
- Get ajaxForm response value
- Update a parent theme JQuery property in the child theme
- jQuery search function not running in WordPress but running locally (fiddle)
- Refresh widget admin page resetting jQuery hidden class
- Can’t seem to get JQuery working (wordpress plugin)
- Ajax Request with jQuery not happening
- Console shows ReferenceError: jQuery is not defined, no clue why!
- slide change on hover with nextgen scrollgallery [closed]
- How to make jQuery load AFTER plugin’s script?
- jQuery not working internal pages [duplicate]
- jQuery .change() Event Bubbling in WP Admin
- Unable to understand this jquery code [closed]
- Combo box populating a DIV using ajax/jquery
- How to Add jQuery cod in wordpress [duplicate]
- can’t make jQuery work (change image on time interval) [closed]