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 3 of 3
  1. #1
    New Coder
    Join Date
    Feb 2012
    Location
    Finland
    Posts
    59
    Thanks
    3
    Thanked 9 Times in 9 Posts

    If finds two <a> elements ingore .append (jQuery)

    Hello!

    I got this code and its works perfectly for containers with only one a element;

    Code:
    <script type="text/javascript">// <![CDATA[
    $(document).ready(function() {  
    var hrefLink = $('#wrapper .top-content1 table a').attr('href');
    $('#wrapper .top-content1').before($('<a></a>').attr({
    style: 'position: absolute; width: 100%; height: 100%;',
    href: ''+ hrefLink +''
    }));
    });
    // ]]></script>
    But what I need to get here is that if there are 2 <a> elements inside table it won't append the new <a> there so this code wouldn't function if two a's have been found

    Code:
    $('#wrapper .top-content1').before($('<a></a>').attr({
    style: 'position: absolute; width: 100%; height: 100%;',
    href: ''+ hrefLink +''
    }));
    If post is too unclear please let me know, I will try my best to clarify it!

    -Riku

  • #2
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    check out this function:

    http://api.jquery.com/each/

  • #3
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Something like this:

    Code:
    <script type="text/javascript">// <![CDATA[
        $(document).ready(function() {
        var aLinks = $('#wrapper .top-content1 table a');
        if (aLinks.length == 1) {        // or some other condition(?)
            var hrefLink = aLinks.first().attr('href');
            $('#wrapper .top-content1').before($('<a></a>').attr({
                style: 'position: absolute; width: 100%; height: 100%;',
                href: ''+ hrefLink +''
            }));
        }
    });
    // ]]></script>
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS


  •  

    Posting Permissions

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