1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Help with a jQuery issue please?

Discussion in 'Other Languages' started by Conor, Nov 12, 2014.

  1. Conor

    Conor Jr. VIP Jr. VIP

    Joined:
    Nov 7, 2012
    Messages:
    3,373
    Likes Received:
    5,439
    Gender:
    Male
    Location:
    South Africa
    Home Page:
    This script doesn't work in Wordpress, and I can't figure out why.

    Code:
    jQuery(".form input").change(function() {
    $('.content p:contains("Custom Text")').hide();
    });
    
    Whenever a form input is modified, I would like to check if certain text exists in an element, and hide that element if the text is found.

    I get an unhelpful "Uncaught TypeError: undefined is not a function" for some reason.
     
  2. MrBlue

    MrBlue Senior Member

    Joined:
    Dec 18, 2009
    Messages:
    950
    Likes Received:
    662
    Occupation:
    Web/Bot Developer
    Wrap your jQuery code in this:
    Code:
    jQuery(document).ready(function ($) {
    
    // your code
    
    });
    
     
    • Thanks Thanks x 1
  3. kosher_12

    kosher_12 Newbie

    Joined:
    Aug 9, 2011
    Messages:
    25
    Likes Received:
    11
    try this

    Code:
    function($){ 
    	$(".form input").change(function() {
    		$('.content p:contains("Custom Text")').hide();
    	});
    })(jQuery)
    
     
  4. Conor

    Conor Jr. VIP Jr. VIP

    Joined:
    Nov 7, 2012
    Messages:
    3,373
    Likes Received:
    5,439
    Gender:
    Male
    Location:
    South Africa
    Home Page:
    Thanks for the responses so far guys. I should have mentioned that I have tried noconflicting the jquery in every possible way I could think of. While it did get rid of the error, it didn't do anything for the functionality of the script.

    What's confusing me is the fact that this code works:

    Code:
    jQuery(".form input").change(function($) {
    alert('Changed');
    });
    
    
    and this code works:
    Code:
    jQuery(document).ready(function ($) {
    $('.content p:contains("Custom Text")').hide();
    });
    
    But they don't work when I combine them.


    I invite anyone to try the same thing on any Wordpress site, to see what I mean.
     
    Last edited: Nov 12, 2014
  5. MrBlue

    MrBlue Senior Member

    Joined:
    Dec 18, 2009
    Messages:
    950
    Likes Received:
    662
    Occupation:
    Web/Bot Developer
    "...But they don't work when I combine them..."

    Try this:
    Code:
    jQuery(document).ready(function ($) {
         $(".form input").change(function(){  
              $('.content p:contains("Custom Text")').hide();
         });
    });
    
     
    Last edited: Nov 12, 2014
  6. Istenais

    Istenais Junior Member

    Joined:
    Sep 1, 2012
    Messages:
    138
    Likes Received:
    39
    Location:
    Latvia, Riga
  7. Conor

    Conor Jr. VIP Jr. VIP

    Joined:
    Nov 7, 2012
    Messages:
    3,373
    Likes Received:
    5,439
    Gender:
    Male
    Location:
    South Africa
    Home Page:
    I managed to get an unnecessarily long piece of code working, if anyone is interested:

    Code:
    jQuery(document).ready(function ($) {
    $('html').click(function () {
    $('.content p:contains("Custom Text")').hide();
    });
    
    
    $('.form input[type="text"], .form textarea').on('input', function() {
    setTimeout(function(){
    $('.content p:contains("Custom Text")').hide();
    }, 500);
    });
    });
    
    I had to add the timeout function because there was other jquery firing before my code that apparently canceled the thing out.