1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

The best way to check for a value.

Discussion in 'jQuery' started by Michael Jackura, Aug 27, 2019.

  1. #1
    First of all, I am another one of those newbies that keeps on seeing in the doc that jquery is easy, so I am starting to feel dumb.
    I would like to know the best way to check a field for value and if it is a certain value change it to a blank or null and if it is blank leave it.
    The other ? I have is how the code is executed. When will that input field get checked for the value when the form is on the screen? Is it always being checked or ....?
    Thanks for answering this beginner.
    mjj4golf
     
    Michael Jackura, Aug 27, 2019 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,494
    Likes Received:
    4,457
    Best Answers:
    123
    Trophy Points:
    665
    #2
    First up, JQuery isn't easy if you aren't a programmer who knows a couple of languages, that experience of picking up a new way of doing things is invaluable. Yes, JQuery is javascript but it has a whole different way of doing things. Based on your post here's a wee primer.

    Lets start with an input field that would be part of a form, or a search box etc.
    <input type='text' name='myField' id='myField'>
    Code (markup):
    Now we'll add a listener to see if the field gets changed and an anonymous function for when there is a change. If change isn't the right keyword for your situation there's keyup, check them out.
    $(document).ready(function(){
       $('#myField').on('change', function(){
          if ($(this).val() !== ''){
             $(this.val('');
          }
       });
    });
    
    Code (javascript):
    This example is really, really simple and doesn't warrant the use of JQuery although an everything in JQuery approach may make future maintenance easier to pick up and put down.

    Here's how you'd do it in pure javascript
    
    document.getElementById("myField").addEventListener("change", checkValue);
    function checkValue(){
       var fld = document.getElementById("myField");
       if (fld.value !== ''){
          fld.value = '';
       }
    }
    Code (javascript):
     
    sarahk, Aug 27, 2019 IP
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #3
    jQuery is NOT easier than vanilla JavaScript, anyone telling you so likely doesn't know enough JavaScript to even have a valid opinion on the subject. This is true of most if not all front-end "frameworks" as they universally tend to be cryptic, convoluted train wreck laundry lists of how NOT to build a website.

    But then I'd even say that the example @sarahk gave could be better done in vanilla no-frills JavaScript. One of the things that makes jQuery stupid is constantly using its querySelectorAll style methods "$(selector)"for things you already should have, or replicating things that should already exist. See their event handler where $(this) is the same as Event.currentTarget.

    Hence:

    
    document.getElementById('myField').addEventListener('change', checkValue, false);
    function checkValue(event) {
      if (event.currentTarget.value !== '') event.currentTarget.value = '';
    }
    
    Code (markup):
    Though I don't think that action is what you're asking for... I think you mean something more like:

    
    if (event.currentTarget.value == 'moot') event.currentTarget.value = '';
    
    Code (markup):
    So that if someone typed in "moot" it would blank the value... correct?

    A lot of people bitch that flat normal JavaScript is too verbose. What's wrong with verbose if it actually explains what's going on? In most cases you end up with roughly the same amount of code, and if that brings with it clarity then do it. In a LOT of cases you can end up with less code if you implement things properly overall, doing things like SIF/IIFE, not waiting for document.ready and instead running ALL your scripting right before </body>, etc, etc.

    The loss of code clarity due to the cryptic syntax, replication of existing functionality, use of procedures for things that already exist like their dumbass val() on the elements, replication of things events already provided such as event.currentTarget, all adds up to a more convoluted hard to digest codebase that even worse, is slow as hell.

    You're not stupid. jQuery is stupid; As are most of the people who say it offers any benefits or even has any blasted business on websites.

    Those who use it by choice have more in common with Easy E than competent developers. They seem like giving up the drawers, bent over their website, with no vaseline.

    If they was smart as me,
    John Resig would be hangin' from a tree.
    With no vaseline,
    just a match and a little bit o' gasoline.
    Light e'm up, burn 'em up, flame on.
    'Till that goofy beard is gone.
     
    deathshadow, Aug 30, 2019 IP
  4. Michael Jackura

    Michael Jackura Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #4
    Thank you both for the great info! Appreciate it.
     
    Michael Jackura, Aug 30, 2019 IP
  5. sarahk

    sarahk iTamer Staff

    Messages:
    28,494
    Likes Received:
    4,457
    Best Answers:
    123
    Trophy Points:
    665
    #5
    Thatl was dark, I had to google it!
     
    sarahk, Aug 30, 2019 IP
  6. Michael Jackura

    Michael Jackura Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #6
    OK, I've been trying to implement the code below in a child function.php file and when I try to go to the website, I get a msg indicating a 'technical problem'. The error.log shows
    [Sun Sep 01 12:17:34 2019] [warn] [client 24.206.45.14] mod_fcgid: stderr: PHP Parse error: syntax error, unexpected '(', expecting variable (T_VARIABLE) or '{' or '$' in /home/dh_dc9dh6/mjj.stbnc.net/wp-content/themes/twenty-nineteen-child/functions.php on line 15
    If I remove the code in red, it works. I matched up all the brackets and parenthesis with no help. Anyone see the problem? Thanks!

    <?php
    add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
    function my_theme_enqueue_styles() {
        $parent_style = 'twentynineteen-style'; // This is 'twentynineteen-style' for the Twenty nineteen theme.
        wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
        wp_enqueue_style( 'child-style',
            get_stylesheet_directory_uri() . '/style.css',
            array( $parent_style ),
            wp_get_theme()->get('Version')
        );
    }
    [COLOR=#ff0000]$(document).ready(function(){           [/COLOR][COLOR=#000000][B]   <-------- Line 15[/B][/COLOR]
    [COLOR=#ff0000]   $('#fld_2416493_1').on('click', function(){
          if ($(this).val() === " "){
             $(this.val("Mike"));
          }
       });
    });[/COLOR]
    ?>
    Code (javascript):
     
    Michael Jackura, Sep 1, 2019 IP
  7. sarahk

    sarahk iTamer Staff

    Messages:
    28,494
    Likes Received:
    4,457
    Best Answers:
    123
    Trophy Points:
    665
    #7
    code in function.php doesn't need to check if the document is ready - it won't be run until it is.
     
    sarahk, Sep 1, 2019 IP
  8. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #8
    The top half looks like PHP, the bottom half is clearly JavaScript / the train wreck of developer incompetence and ineptitude that is jQuery. They USUALLY don't belong in the same file.

    Try moving the ?> to before the scripting, then doing <?php after if there is PHP after it. That way its treated as output. You could also use echo'' to output the scripting client-side.

    Though it probably also needs <script> tags assuming those are not already present elsewhere in the output.

    
    <?php
    add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
    function my_theme_enqueue_styles() {
        $parent_style = 'twentynineteen-style'; // This is 'twentynineteen-style' for the Twenty nineteen theme.
        wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
        wp_enqueue_style( 'child-style',
            get_stylesheet_directory_uri() . '/style.css',
            array( $parent_style ),
            wp_get_theme()->get('Version')
        );
    }
    
    ?>
    	<script>
    		$(document).ready(function(){
    			$('#fld_2416493_1').on("click', function(){
    					if ($(this).val() === ' '){
    						 $(this.val('Mike'));
    					}
    			 });
    		});
    	</script>
    
    <?php
    Code (markup):
    Though again, this really doesn't/shouldn't be in the markup that PHP is outputting. It belongs in an external .js file that would/should be loaded right before </body> so the document.ready nonsense can be skipped altogether. Though good luck convincing the train wreck laundry list of how NOT to build a website that is Turdpress of that.

    PHP runs server-side. JavaScript runs client side, you shouldn't be mixing and matching like that. Good scripting should hook the markup from an external file, not be slopped into the markup like that.

    Basically, it's got problems.
     
    deathshadow, Sep 2, 2019 IP
    malky66 and sarahk like this.
  9. Tim Werfel

    Tim Werfel Greenhorn

    Messages:
    49
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    23
    #9
    Thanks for sharing ! will help to a lot of us ;)
     
    Tim Werfel, Sep 2, 2019 IP