Ho can i find specific div in javascript?? Plz help

Discussion in 'JavaScript' started by MrMalik, Aug 6, 2009.

  1. #1
    I have lot of divs in my main.aspx webpage and there is a div that's id is div_main, Now i want to find that div_main div. Actually i am using a master page and there i write two javascript fuctions. Now in my first javascript function, i want to find that div so that if i get it then second javascript fuction shouldn't execute else it should execute. There is a some important code in my second javascript function that i want to execute in all pages rather than main.aspx web page.

    Plz plz give me quick answer that how can i find div in javascript
     
    MrMalik, Aug 6, 2009 IP
  2. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Rather basic..

    
    if (document.getElementById('div_main')) {
      //it exists..
    } else {
      //not found
    }
    
    Code (markup):
     
    premiumscripts, Aug 6, 2009 IP
  3. swashata

    swashata Member

    Messages:
    86
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #3
    Or you can also use jQuery, if you already have the framework!! Use it like this:
    
        var myDiv = $('#'+htmlid);
        if(myDiv.length!=0) //This div is present! You function call here;
        else //This div is absent! Your function call here;
    
    Code (markup):
    Check this demo page! http://itg.intechgrity.com/jquery/jquery-selector.html I hope you like this :) The source code is ==>
    
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>Sample Page for jQuery selectos</title>
        <!-- include the jQuery framework -->
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
        <!-- Now make a function to use the framework -->
        <script type="text/javascript">
        function jQuerySelector(htmlid){
            var myDiv = $('#'+htmlid);
            if(myDiv.length!=0) alert("This Div having id "+htmlid+" is present!");
            else alert("This div having id "+htmlid+" is absent!");
        }
        </script>
    </head>
    <body>
        <div id="swashata">Page by <a href="http://www.intechgrity.com">inTechgrity</a>! Check the source code to see how things work!</div>
        <script type="text/javascript">
        jQuerySelector("swashata");
        jQuerySelector("yourname");
        </script>
    </body>
    </html>
    
    Code (markup):
     
    swashata, Aug 6, 2009 IP
  4. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #4
    this last post re:jquery is just detached from reality... i'd compare it to picking up a sledge hammer to put a picture up on the wall...

    premium scripts is right and as per ECMA specifications, failure to locate the element returns a "false". (myDiv.length!=0) ???? please...
    LEARN / READ UP ON JAVASCRIPT BEFORE JQUERY.

    p.s. sorry this is not personal, just getting fed up of all the generic jquery solutions and jquery experts...
     
    Last edited: Aug 6, 2009
    dimitar christoff, Aug 6, 2009 IP
    premiumscripts likes this.
  5. swashata

    swashata Member

    Messages:
    86
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #5
    hmmm, I just tried to figure out the possibilities! Many people are using jQuery so just wanted to state how it works on jQuery :) I know JavaScript also and thats why added a OR before my post! You are absolutely rite! I visited your blog and must say that this is indeed a reality! But, dont you think that after including the 55kb framework, we can actually do the same thing by writing less of codes :)
    Nothing personal from me either! I just like jQuery and may be this discussion can help me learn more buddy :)
     
    swashata, Aug 6, 2009 IP
  6. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Haha, exactly. After including 55kb of code we can do the same thing with less code.. Obviously alot of that code will need to be parsed and the result will be that the site will be alot slower, at least for something as simple as this. If the required javascript was more complex it would be more wise to recommend jquery, but as it is, no.
     
    premiumscripts, Aug 6, 2009 IP
  7. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #7
    well that's fair enough - i don't mean to come across as some random prick. :D

    i just learnt something about jquery I did not know about before... it does NOT return false if the selector comes empty. instead, it's primed to return an array hence what you said is correct, I must apologise. i guess jquery just can't return false, if chaining is to work it needs to return the jquery object.

    in vanilla js or mootools or prototype, what I said about return value being false would be true due to element.prototype

    hence for jquery this is valid: if (!$("#someid").length) alert("someid not found");
    for mootools all you'd do is if (!$("someid")) alert("someid not found");

    sorry once again! :)
     
    dimitar christoff, Aug 6, 2009 IP
  8. mrmaf

    mrmaf Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Hi MrMalik ! You can get div with this javascript built-in function document.getElementsByTagName("div"); Here's the best solution that i found over internet related problem that you are facing How to find div in javascript
     
    mrmaf, Aug 7, 2009 IP
  9. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Why do people supply worse answers when the answer has already been given? :)
     
    premiumscripts, Aug 7, 2009 IP
  10. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #10
    if the person that started the thread would come in and say thanks or something, people would just back off - but since it's a relatively easy question that's still open, it offers a chance to lot of people (who _feel_ equipped to answer this) to come in and present their point of view... :)

    If you're bored, see if you can help on a question like this one here on animation performance in IE: http://forums.digitalpoint.com/showthread.php?t=1444499

    little point in chewing over the 'hello worlds' of javascript :)
     
    dimitar christoff, Aug 7, 2009 IP
  11. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #11
    You answered most of that topic already, it's now waiting for the original poster to reply and see if any of it helped. :) Wouldn't really be able to help in such a case anyway, as I'm not much of a javascript expert. I hate browser oddities ;)
     
    premiumscripts, Aug 7, 2009 IP
  12. swashata

    swashata Member

    Messages:
    86
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #12
    What you prefer to be more effective and why? :) I mean which JS Framework?
     
    swashata, Aug 7, 2009 IP
  13. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #13
    http://jqueryvsmootools.com/ - this page reflects a lot of my sentiments on the subject. i guess it's down to personal preference but the nutshell of it is:

    - if you know javascript / object orientated programming (oop) then mootools is the better choice by far (moo stands for my object orientated and is amazing for class-based programming, inheritance / extending).
    - if you want to learn javascript and use a framework anyway, mootools is the better choice by far, instead of the css-like shortcuts used by jquery, it actually uses native and extended/improved js commands by changing prototypes and implementing things that are due to come in standard js already.
    - if you want to get access to the DOM elements and not have to bother learning much else, then jquery is the obvious solution, it is the fastest and easiest way to get things done. learning curve for mootools is much steeper.
    - if you want access to a lot more plugins and community support for your framework, go with jquery.

    this is not to say that what jquery does cannot be done in mootools, it can. a lot of what mootools does cannot be done in jquery (we're talking the cores of the two frameworks, not some 3-rd party plugins which may stop to exist).

    also, this is not to say there is a lack of plugins / code for mootools, just that jquery has much more (as is right, they have over 40% of the framework market share and growing whereas mootools is like 11%).

    each to their own - read the article posted though - it really will highlight why people choose a framework in the first place and how far you can take it...
     
    dimitar christoff, Aug 7, 2009 IP