-1

I found a way to split a string into an array as per How do I split a string, breaking at a particular character?. Unfortunately, this doesn't seem to work when getting the string from a function.

In it, the string is made from a variable.

var input = 'John Smith~123 Street',
    array = input.split(/~/);

This would make array[0] = 'John Smith' and array[1] = '123 Street'.

When I try make this with a variable from a function, it doesn't work as shown below.

<input type=button onclick="fn('John Smith~123 Street')" value=clickme>

function fn(stringval)
    {
        var array = stringval.split(/~/);
    }

Does anyone know why this happens and how to get out of it? I tried going var input = stringval; but that didn't work. If you answer, I can use jQuery or Javascript.

Thanks!

EDIT:

This is exactly what it is doing:

<h2 class=opt onclick="tree('Basic Formatting',1,'#html~HTML');">

function tree(i,j,k)
    {
    var a = k.split(/~/);
    if (j==1)
        {
            $('#path').html('<span onclick="pageLoad(\'.lang\')">Home</span> > '+'<span onclick="pageLoad(\''+a[0]+'\')">'+a[1]+'</span> > '+i);}
        }

I am attempting for it to result in:

<div id=path>
    <span onclick="pageLoad(\'.lang\')">Home</span>
    >
    <span onclick="pageLoad(\'#html\')">HTML</span>
    > Basic Formatting
</div>
Community
  • 1
  • 1
Fin
  • 353
  • 3
  • 5
  • 16
  • Your function doesn't use or return the `array` variable after the assignment - that's where the problem is, not with `.split()`. – nnnnnn Apr 22 '17 at 02:15
  • works fine inside function here https://jsfiddle.net/7eq98who/ . You don't do anything with that variable as currently shown. What are you wanting the function to do – charlietfl Apr 22 '17 at 02:15
  • @nnnnnn even if it did return a value, it would be silently discarded – Tibrogargan Apr 22 '17 at 02:15
  • the string ``stringval`` represents information separated by a tilda (~) and the split is mean't to make the tildas make an array... isn't it? – Fin Apr 22 '17 at 02:16
  • Finley, what did you want to actually do with the array? – Tibrogargan Apr 22 '17 at 02:16
  • 1
    Yes, but what do you want to do with the resulting array? In the code shown you don't use the results at all. Please [edit] your question to explain what the desired result is. – nnnnnn Apr 22 '17 at 02:17
  • It makes an array, but then you just [implicitly] throw the array away – Tibrogargan Apr 22 '17 at 02:17
  • The array is going to split the two sets of data (in the example 'John Smith' and '123 Street') and make them usable through array[0], etc. – Fin Apr 22 '17 at 02:18
  • to see if `array` is really created and what's inside it? `function fn(stringval) { var array = stringval.split(/~/); console.log(array); }` – Hamza Rashid Apr 22 '17 at 02:18
  • 1
    This is an [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) . You need to explain on higher level exactly what it is you are trying to accomplish – charlietfl Apr 22 '17 at 02:19
  • ok I'll update the question to be exactly specific to my program – Fin Apr 22 '17 at 02:20
  • 1
    The code in the updated question works: https://jsfiddle.net/0p2ukvnb/ - assuming you have included jQuery.js. If it doesn't work for *you* then perhaps you haven't included your ` – nnnnnn Apr 22 '17 at 02:29

1 Answers1

-1

All that effort then I figure it out myself.

function fn(i,j,k)
    {
        var input = String(k),
            a = input.split(/~/);
    }
Fin
  • 353
  • 3
  • 5
  • 16
  • No, because the code in the update to your question works as is. `k` was already a string. – nnnnnn Apr 22 '17 at 02:32
  • `k` is already a string... as shown in nnnnn demo the code you updated question with already works. Your issue must have been something else. Strongly suggest using console to check errors and report those in future questions. You probably had one being thrown somewhere – charlietfl Apr 22 '17 at 02:32