Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  • Thread Tools
  • Rate This Thread
  1. #1
    Senior Coder
    Join Date
    Dec 2002
    Location
    Arlington, Texas USA
    Posts
    1,105
    Thanks
    4
    Thanked 11 Times in 11 Posts

    juery equivalent to this function

    I am trying to get the hang of jQuery

    I have the following functions

    function show(o){
    var e = document.getElementById(o);
    e.style.display = 'block';
    }
    function hide(o){
    var e = document.getElementById(o);
    e.style.display = 'none';
    }


    how do I do this in jQuery when I do not know the id for the div in question?
    Last edited by miranda; 02-23-2016 at 04:44 PM. Reason: was not finished hit enter key by mistake

  2. #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    8,055
    Thanks
    2
    Thanked 808 Times in 797 Posts
    The jQuery version would not be much different in length so you might as well use that version of the code.

    Using jQuery is only worthwhile where you have thousands of lines of code and jQuery will save you a hundred or so lines (although these days its hard to find a situation where the saving would be that much).
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  3. #3
    Regular Coder
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    311
    Thanks
    0
    Thanked 42 Times in 40 Posts
    Quote Originally Posted by felgall View Post
    (although these days its hard to find a situation where the saving would be that much).
    Or any days really, which is part of why I'm so shocked that anyone actually bothers TRYING to use it for much of anything.

    This might be an odd question but what triggers those show/hide functions? If it's a clickable event then MAYBE it should be JS (there are ways around that now) but if it's a hover state, my question would be why even have JavaScript involved? Even with JavaScript involved I'd be swapping classes not directly accessing style, since who knows if that style is even applicable in all the different media targets and / or media queries a site might have.

    Good rule of thumb? If you are using Element.style in your JavaScript, in all but the rarest of corner cases you are doing something wrong.

    But I'd have to see what is trying to call those to weight in more.

    Though for completeness sake the jQuery equivalent SHOULD be:
    Code:
    function show(o){
      $('#' + o).css('display', 'block');
    }
    function hide(o){
      $('#' + o).css('display', 'none');
    }
    I'm guessing slightly though -- my experience with jQ was just long enough to go "what is this needlessly agonizingly cryptic hard to follow rubbish?!?" before moving it to the top of my naughty list where it sat for over six years, until just a couple years ago a new upstart called "bootstrap" went and outdid it on the herpaderp scale.

  4. #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    8,055
    Thanks
    2
    Thanked 808 Times in 797 Posts
    Quote Originally Posted by deathshadow View Post
    Or any days really, which is part of why I'm so shocked that anyone actually bothers TRYING to use it for much of anything.
    I( am starting to see threads on various forums where people are asking for the vanilla JavaScript equivalent to various jQuery commands because they now want to get rid of jQuery (since now that IE8 is dead the cross browser support it used to provide is no longer required plus JavaScript itself now has equivalent commands for most things).

    I can see jQuery in the future shrinking to be just wrappers around the equivalent JavaScript commands for those who haven't got around to reqriting their code to get rid of jQuery yet.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  5. #5
    Regular Coder
    Join Date
    Sep 2013
    Location
    Houston
    Posts
    119
    Thanks
    5
    Thanked 21 Times in 21 Posts
    how do I do this in jQuery when I do not know the id for the div in question?
    In jquery you need to know some sort of selector so if its not by ID you would use class but you need to make sure that you only have one of that class type in the dom (which somewhat defeats the purpose of using class).
    PHP Code:
    $('.some-class').hide();
    $(
    '.some-class').show(); 
    As for the rest of the thread I would think generally productivity would be huge reason why companies and individual users have jQuery. Why bother with 3 different ways to query something when jQuery gives you the ability to do it with one simple system. Also the chaining is very useful. If you want to use the same type of strength that the prototype chaining gives you in jQuery you are going to have to write your own wrappers around the JS native functions to make them functional and return the new instance of the object.

    Ultimately I wouldn't spend my time rewriting functions that already exist because I'd rather do things that are fun when programming which are solving new problems.

    Either way this isn't much of a concern for our Apps at work because we've moved on to react so direct dom manipulation has become a bit of bad practice, but I don't see anything wrong with people using jQuery for the convenience. If including jQuery in your website/mobile application is the difference between good and bad load time you are already doing something else incredibly wrong.
    if(poster === Lesshardtofind){
    output = !certified;
    }

  6. #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    8,055
    Thanks
    2
    Thanked 808 Times in 797 Posts
    Quote Originally Posted by Lesshardtofind View Post
    Why bother with 3 different ways to query something when jQuery gives you the ability to do it with one simple system. Also the chaining is very useful.
    I's substitute JavaScript into that statement instead of jQuery.

    The only difference is that the jQuery provides a slightly shorter wrapper around a simple JavaScript statement. For the chaining, single elements chain fine in JavaScript, for multiple elements you need a callback. Anyway, all jQuery is, is a rapidly shrinking chunk of JavaScript code.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  7. #7
    Regular Coder
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    311
    Thanks
    0
    Thanked 42 Times in 40 Posts
    Quote Originally Posted by Lesshardtofind View Post
    Why bother with 3 different ways to query something when jQuery gives you the ability to do it with one simple system. Also the chaining is very useful. If you want to use the same type of strength
    Which methinks means we hast a different definition of the words "simple", "useful" and "strength".

    jQuery is needlessly cryptic, pointlessly bloated, and just more crap to learn which is why most people using it don't seem to know enough JavaScript, HTML or CSS to even be making websites in the first blasted place. It sat at the top of my "are people REALLY this stupid" list for as long as it did for that alone.

    Much less "chaining" just results in long hard to follow lines of "wait, just what the *** are we operating on again?". That in most cases it's people using JS to do CSS' job only adds insult to injury on that.

    It makes people write more code, that relies on more code, often to do things that have no business being coded... so where's this ALLEGED simple, useful and strength coming from?!?

    EVERYTHING I've ever seen done with jQuery falls into one of three categories:

    1) something that would be simpler, easier to follow, or less code without jQuery, NOT even counting the size of the framework against the result.

    2) Stuff that's CSS' job.

    3) Things that have ZERO blasted business on websites in the first place!

    Quote Originally Posted by Lesshardtofind View Post
    Either way this isn't much of a concern for our Apps at work because we've moved on to react so direct dom manipulation has become a bit of bad practice
    I've been dealing with a few things built with react.js, and everything I've dealt with leaves me wanting to punch someone in the face! It reeks so badly of "HTML, what's that?", "DOM, what's that do?" and "CSS, what's that do?" that the bloated idiotic MESSES it vomits up are little more than telling visitors or users of anything built with it to go **** themselves from an accessibility or usability standpoint; much less a speed, efficiency or simplicity of maintenance standpoint.

    Needlessly pointlessly convoluted rubbish that blatantly shows whoever created it has no business making websites or applications. Much like jQuery I stand in wonder of how anyone could delude themselves into seeing any sort of advantage to that nonsense.

    Though to be fair, I say the same thing about pretty much all frameworks as to me they universally reek of the folks using them not understanding HTML, CSS, JS or accessibility enough to even have an informed opinion on the topic!

    There's a joke I usually make, but I'm unsure how these forums feel about making fun of faith or pseudo-science quackery.

    Quote Originally Posted by Lesshardtofind View Post
    but I don't see anything wrong with people using jQuery for the convenience.
    Yeah, I do believe we have a different definition of "convenience" as well. I dunno, maybe I've just watched too many projects I used to support flush themselves down the toilet with it. (yes SMF, I'm looking at you!)

    Quote Originally Posted by Lesshardtofind View Post
    If including jQuery in your website/mobile application is the difference between good and bad load time you are already doing something else incredibly wrong.
    Like using jQuery -- though more likely it's a nasty case of "JavaScript for NOTHING!"

    JS for nothing and your scripts for free... that ain't workin', that's not how you do it. Lemme tell ya, these guys ARE dumb. Maybe pop the enter key off your little laptop, maybe break the shift key with your thumb. We got to install some PHP now, custom CMS, delivereeeeeeey. We got to backup these, sql tables. We got to backup our PHPeeeeee....

    I want my, I want my, I want my PHP.

  8. #8
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    9,808
    Thanks
    6
    Thanked 1,159 Times in 1,130 Posts
    Can we please stop taking threads to off-topic debates on principles? The OP didn’t ask for a lengthy rant on why jQuery is worse than native JS.

  9. #9
    Regular Coder 2reikis's Avatar
    Join Date
    Nov 2005
    Location
    New Mexico, USA
    Posts
    182
    Thanks
    21
    Thanked 13 Times in 13 Posts
    function show(o) {
    $('#'+o).show();
    }

    function hide(o) {
    $('#'+o).hide();
    }
    2Reikis
    I did not arrive at my understanding of the physical universe through my rational mind.
    Albert Einstein


 

Tags for this Thread

Posting Permissions

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