Web Analytics Made Easy -
StatCounter Split a String using Jquery or Javascript - CodingForum

Announcement

Collapse
No announcement yet.

Split a String using Jquery or Javascript

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Split a String using Jquery or Javascript

    Hoping someone can help.

    Using K2 Designer expressions, how can i split the following address (it is contained in a TEXT BOX) into seperare parts?

    123 K2 Avenue,K2Town,K2County,K21456


    Unfortunately there is no SPLIT function when using K2 Designer
    Is there a way of doing it using Javascript in an expression?

    I have four textboxes containing the above string, can anyone show me how to use javascript to just display the separate address parts?


    Thanks guys,

  • #2
    I have never heard of K2 Designer, but try this:-


    Code:
    <script type = "text/javascript">
    
    var str = "123 K2 Avenue,K2Town,K2County,K21456";
    var strarray = str.split(",");
    alert (strarray[0] +  "\n" + strarray[1] + "\n"  + strarray[2]  + "\n" + strarray[3]);
    
    </script>
    It's opaque, you can see right ot the bottom of the pool. - Commentator BBC Radio 1
    Last edited by Philip M; Sep 22, 2016, 11:34 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

    Comment


    • #3
      Originally posted by Philip M View Post
      I have never heard of K2 Designer, but try this:-


      Code:
      <script type = "text/javascript">
      
      var str = "123 K2 Avenue,K2Town,K2County,K21456";
      var strarray = str.split(",");
      alert (strarray[0] +  "\n" + strarray[1] + "\n"  + strarray[2]  + "\n" + strarray[3]);
      
      </script>
      It's opaque, you can see right ot the bottom of the pool. - Commentator BBC Radio 1
      Hi Phil,

      Firstly, thanks for your reply.
      I tried the code and it works fine and I see a pop-up alert on my screen using your code. I tried to modify the code, but I can't get it to work when I set your line var str = my passed in Parameter (which is a string) from K2.

      This is how K2 can use JavaScript: How to call javascript in k2 smartforms? - K2 Community

      Can your script allow me to pass in a variable? and then put the result back in the Text Box from where I am running it? I use the above K2 method to use an expression to run small JavaScript code.

      Hope that makes sense.

      Comment


      • #4
        Originally posted by sharpharp View Post
        Hi Phil,

        Firstly, thanks for your reply.
        I tried the code and it works fine and I see a pop-up alert on my screen using your code. I tried to modify the code, but I can't get it to work when I set your line var str = my passed in Parameter (which is a string) from K2.

        This is how K2 can use JavaScript: How to call javascript in k2 smartforms? - K2 Community

        Can your script allow me to pass in a variable? and then put the result back in the Text Box from where I am running it? I use the above K2 method to use an expression to run small JavaScript code.

        Hope that makes sense.
        As I say, I have no knowledge of K2. What is the value of your passed in parameter (a string) when using my code?

        All the code given in this post has been tested and is intended to address the question asked.
        Unless stated otherwise it is not just a demonstration.

        Comment


        • #5
          Originally posted by Philip M View Post
          As I say, I have no knowledge of K2. What is the value of your passed in parameter (a string) when using my code?
          No worries, I managed to do it using a long winded way without JavaScript using multiple textboxes and left find expressions to capture each part of the string.

          Thank you anyway for your help

          Comment


          • #6
            Originally posted by sharpharp View Post
            No worries, I managed to do it using a long winded way without JavaScript using multiple textboxes and left find expressions to capture each part of the string.

            Thank you anyway for your help
            Glad to hear that your problem is solved.

            These code generators are often a big headache - it is best to use plain vanilla Javascript.

            All the code given in this post has been tested and is intended to address the question asked.
            Unless stated otherwise it is not just a demonstration.

            Comment

            Working...
            X