Page 1 of 3 123 LastLast
Results 1 to 10 of 29

Thread: jQuery if Element is Found

  1. #1
    Join Date
    Oct 2012
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default jQuery if Element is Found

    I'm just starting jQuery and have the basics down and would like to use the "if" statement. You can show me the code or link to a tutorial or example coding.

    How would I code jQuery to do the following (I know it has to be the 'if' statement, but not sure how to code it:

    When the LINK "Books" is clicked -- If .wrapper element contains a .Book element anywhere in it; Example:

    HTML Code:
    <a href="#" id="book">Book</a> | <a href="#" id="magazine">Magazine</a>
    <br />
    <div class="wrapper">
    <img src="#" class="book" />
    </div>
    <div class="wrapper">
    <img src="#" class="book" />
    <img src="#" class="magazine" />
    </div>
    <div class="wrapper">
    <img src="#" class="magazine" />
    </div>
    Remove/hide all .wrapper that do not have .book inside the .wrapper. So the above HTML would end looking like:

    HTML Code:
    <a href="#" id="book">Book</a> | <a href="#" id="magazine">Magazine</a>
    <br />
    <div class="wrapper">
    <img src="#" class="book" />
    </div>
    <div class="wrapper">
    <img src="#" class="book" />
    <img src="#" class="magazine" />
    </div>
    v/Respectfully
    Kage
    Last edited by Kage Kazumi; 10-15-2012 at 09:13 AM.

  2. #2
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    Just one quick note,
    You can't have more than one div with the same id.
    You should change id="wrapper" to class="wrapper"
    Last edited by keyboard; 10-15-2012 at 05:14 AM.

  3. #3
    Join Date
    Oct 2012
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by keyboard1333 View Post
    Just one quick note,
    You can't have more than one div with the same id.
    You should change id-"wrapper" to class="wrapper"
    What do you mean I can't? You mean jQuery can't?

    ~Kage

  4. #4
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    No; in html, you aren't supposed to have two elements with the same id on one page.
    Last edited by keyboard; 10-15-2012 at 05:22 AM.

  5. #5
    Join Date
    Oct 2012
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by keyboard1333 View Post
    No, in html, you aren't supposed to have two elements with the same id on one page.
    So I need to switch all my wrappers to .wrapper?

    Or can I not have more then one of the same class either?

    ~Kage
    Last edited by Kage Kazumi; 10-15-2012 at 05:29 AM. Reason: Edited

  6. #6
    Join Date
    May 2012
    Location
    Hitchhiking the Galaxy
    Posts
    1,013
    Thanks
    46
    Thanked 139 Times in 139 Posts
    Blog Entries
    1

    Default

    No, because it can cause your code to stop working completely. Take keyboards suggestion and change the id's to classes, or you could have some serious problems in the future.
    "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program." - Linus Torvalds
    Anime Views Forums
    Bernie

  7. #7
    Join Date
    Oct 2012
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by bernie1227 View Post
    No, because it can cause your code to stop working completely. Take keyboards suggestion and change the id's to classes, or you could have some serious problems in the future.
    Okay so then back to the original question.

    Thanks for the tip.

    EDIT: changed my sample HTML to reflect the IDs to Class.

    ~Kage

  8. #8
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    HTML Code:
    <!DOCTYPE html>
    <html>
    <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> 
    <script type="text/javascript">
    $(window).load(function() {
    	$('#book').click(function() {
    		$(".wrapper:not(:has(img.book))").remove();
    	});
    });
    </script>
    </head>
    <body>
    <a href="#" id="book">Book</a> | <a href="#" id="magazine">Magazine</a>
    <br />
    <div class="wrapper">
    <img src="#" class="book" />
    </div>
    <div class="wrapper">
    <img src="#" class="book" />
    <img src="#" class="magazine" />
    </div>
    <div class="wrapper">
    <img src="#" class="magazine" />
    </div>
    </body>
    </html>

  9. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Well it is a specification that in HTML you cannot have more than one element on a page with a given id. But in ordinary HTML and css this rarely if ever causes any problem. In javascript though, if you look for an element with an id and there's more than one, you either get an error or only the first one. jQuery is built on javascript and follows this same model. In both ordinary javascript and jQuery there are ways of looking for elements that will get around this limitation. But in every case it's safer and easier to use class instead of id if multiple elements are involved.

    Oh, and if your page looks like this:

    HTML Code:
    <a href="#" id="book">Book</a> | <a href="#" id="magazine">Magazine</a>
    <br />
    <div id="wrapper">
    <img src="#" class="book" />
    </div>
    <div id="wrapper">
    <img src="#" class="book" />
    <img src="#" class="magazine" />
    </div>
    <div id="wrapper">
    <img src="#" class="magazine" />
    </div>
    before, it will look like that after. You just won't see, when looking at the page in the browser, those elements that have been hidden. They're not actually removed when you use jQuery's .hide() method. They could be if you wanted to do that. It would require another method be used though.

    OK, so let's use this HTML:

    HTML Code:
    <a href="#" id="book">Book</a> | <a href="#" id="magazine">Magazine</a>
    <br />
    <div class="wrapper">
    <img src="#" class="book" />
    </div>
    <div class="wrapper">
    <img src="#" class="book" />
    <img src="#" class="magazine" />
    </div>
    <div class="wrapper">
    <img src="#" class="magazine" />
    </div>
    And, you're probably right about needing if - but only in ordinary javascript. In jQuery you can use its selector syntax and chaining to determine what gets done to which elements (I added text to the divs to make it easier to see what's happening, but if you have real images, you won't need that):

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
    <script type="text/javascript">
    jQuery(function($){
    	$('#book, #magazine').click(function(e){
    		$('.wrapper').hide().has('.' + this.id).show();
    		e.preventDefault();
    	});
    });
    </script>
    </head>
    <body>
    <a href="#" id="book">Book</a> | <a href="#" id="magazine">Magazine</a>
    <br />
    <div class="wrapper">
    <img src="#" class="book" />
    I'm just a book div
    </div>
    <div class="wrapper">
    <img src="#" class="book" />
    <img src="#" class="magazine" />
    I'm a book and magazine one
    </div>
    <div class="wrapper">
    <img src="#" class="magazine" />
    only a magazine here
    </div>
    </body>
    </html>
    Last edited by jscheuer1; 10-15-2012 at 06:10 AM. Reason: code improvement
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. #10
    Join Date
    Oct 2012
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I thank you both, but neither of those worked. I was hoping for something that would be easy to use and simply edit as needed, but I guess their is some kind of issue: http://zanime.net/test/

    ~Kage

Similar Threads

  1. Resolved check if element is in array using jquery
    By ggalan in forum JavaScript
    Replies: 1
    Last Post: 02-12-2012, 03:34 AM
  2. Resolved dynamic DOM jQuery find element
    By ggalan in forum JavaScript
    Replies: 10
    Last Post: 11-27-2011, 09:19 PM
  3. jQuery - Change content of an element
    By RichardG in forum JavaScript
    Replies: 3
    Last Post: 01-30-2011, 03:35 PM
  4. jQuery Image Magnify v1.1 laying under flash element
    By Chicklettes in forum Dynamic Drive scripts help
    Replies: 2
    Last Post: 12-05-2009, 07:43 PM
  5. jQuery hide() and show() based on a select element.
    By nicksalad in forum JavaScript
    Replies: 3
    Last Post: 10-08-2009, 12:28 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •