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 5 of 5
  1. #1
    New Coder
    Join Date
    Dec 2011
    Posts
    26
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Javascript problem with a clause?

    Thank you!
    Last edited by Albana_12; 12-14-2011 at 12:05 PM.

  • #2
    New Coder
    Join Date
    Apr 2010
    Location
    Norfolk, England
    Posts
    63
    Thanks
    1
    Thanked 14 Times in 14 Posts
    Hi Albana_12

    your script gets the last character of the eventId, so i'm presuming in the original eventId thats passed to it, there is a string before the number? like so:

    foo12

    for example.

    if that's correct, then you can simply run through the characters until they are no longer numbers:

    Code:
    function wysiwyg_tools_plus_theme_toggleAccordion… {
    
        var temp = "";
        while(!isNaN(eventId.substring(eventId.length-(temp.length+1), eventId.length)))
        {
    	    temp = eventId.substring(eventId.length-(temp.length+1), eventId.length);
        }
    
        $('#acc-' + temp).toggle('fast');
    
    }
    //Improvement in coding is iterative, each 'failure' is just the next step on your learning curve, some knowledge and logic can get you a long way.//

  • Users who have thanked thesam101 for this post:

    Albana_12 (12-14-2011)

  • #3
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    If you're trying to get a string's numeric suffix of any length, you can do this:

    eventId = eventId.match( /\d+$/ );

  • Users who have thanked Logic Ali for this post:

    Albana_12 (12-14-2011)

  • #4
    New Coder
    Join Date
    Apr 2010
    Location
    Norfolk, England
    Posts
    63
    Thanks
    1
    Thanked 14 Times in 14 Posts
    Quote Originally Posted by Logic Ali View Post
    If you're trying to get a string's numeric suffix of any length, you can do this:

    eventId = eventId.match( /\d+$/ );
    Or just do that lol, damn my lack of regex knowledge
    //Improvement in coding is iterative, each 'failure' is just the next step on your learning curve, some knowledge and logic can get you a long way.//

  • Users who have thanked thesam101 for this post:

    Albana_12 (12-14-2011)

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,174
    Thanks
    75
    Thanked 4,339 Times in 4,305 Posts
    Or, even simpler:
    Code:
    function wysiwyg_tools_plus_theme_toggleAccordion… 
    {
        eventId = eventId.replace(/[^\d]/g, "" );
        $('#acc-' + eventId).toggle('fast');
    }
    Replace everything in the eventId that is *NOT* a digit with blank string. All that is left is/are the digit(s).
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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