SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Apr 2010
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    $('#id') and getElementById

    Hey,

    I'm confused by how $('#id') and getElementById work because I thought getting an element by id would only match once.

    When I do this in jquery:

    Code:
    $('#click_event').click(function() {
      alert("Click event fired: " + $(this).text());
    
      return false;
    });
    
    <a href="#" id="click_event">Text 1</a>
    <a href="#" id="click_event">Text 2</a>
    It correctly shows the text. If it truely only matches once, then wouldn't the second href not fire the event and take you wherever the href is taking you to?

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,595
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    It probably does just that in some browsers.

    How any getElementById processing will work with that code is undefined since such a combination is not allowed in valid HTML and so browsers can do whatever they like with it.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Zealot
    Join Date
    Apr 2010
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So basically what you are saying is that because I'm breaking valid HTML then the rule for matching once no longer applies?

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,595
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    When you start breaking the rules you can't tell what will happen. With that code you could get five different outcomes from five different browsers if each implemented their code in a different way.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Member kompile's Avatar
    Join Date
    Jul 2008
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When I see the source code onhttps://secure.elance.com/php/reg/ma...gnupsId=555209
    Payment method

    How to create getElementById pop up using javascript.
    Please tell me the simple source code. Because I am beginner.
    If it doesn't kill me it can only make me stronger

  6. #6
    SitePoint Enthusiast TriLLi's Avatar
    Join Date
    Feb 2010
    Location
    BiH, Bugojno
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I just want to give you a little start with HTML and javascript.

    Regarding w3c HTML attribute ID needs to be UNIQUE on whole page. That means when you use method document.getElementById(id) first element will be returned because there should be only one with that id. jQuery works same way. Why should they create functionality for invalid HTML.

    But I know what is your need, so I will try to help you.

    You can add another attribute to your element for example attribute "rel" and assign action to all elements with rel attribute and with specific value of that attribute. Example is below:

    Code JavaScript:
     $('a[rel=click]').each(function()
    {
      $(this).click(function() { alert('Senad Meskin, [url]www.wiseblog.info');[/url] });
    });
    this will work for next html
    Code HTML4Strict:
    <a href="#" rel="click" > Clicable</a>
    <a href="#" rel="click" > Clicable</a>
    <a href="#" > Clicable</a>

    it wont work for a third link in HTML above.
    But you need to execute this script when page is loaded, for example
    Code JavaScript:
    $(document).ready(function(){ 
    //put script from above
     });

    I hope this will help you
    http://www.wiseblog.info
    Programmers don't die, they just GO SUB without return.


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
  •