It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming JavaScript Forum

HELP! I don't understand why I can't combine my 2 functions

HELP! I don't understand why I can't combine my 2 functions

Postby js_amnesiac on Thu Apr 09, 2009 9:18 am

I am not an expert with Javascript but I have managed to resolve the majority of my problems of trying to retrieve a detailed string from an array using a part of that string as the search parameter.

The thing that I can't resolve is why I can't get it to work as a single function by joining together the 2 functions I have created.

Here is my code, after the array drirejs has been created by PHP and converted to a JS array:


Code: Select all
<script type="text/javascript">

var rand = new Array();
var john = new Array();

function test_example(){
  for (j = 0; j <= drirejs.length; j++) {
drire_element=drirejs[j];
arrayofparts=drire_element.split("¤");
title=arrayofparts[1];
   john [j]= title;
}
}

function testindex(drire_search){
var srchstr=drire_search;

if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length >>> 0;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

drire_index=john.indexOf(srchstr);
alert(drirejs[drire_index]);
}

</script>
<table>
    <tr>
          <td width="12%" height="123"><script>test_example();</script></td>
          <td width="12%" height="123"><script>testindex("LORRAINE - Vosges(88)");</script></td>
    </tr>
</table>



When the 2 functions are called separately, and consecutively, I get the right result in the alert() from the testindex function.

When I join the 2 functions I don't even get the alert.

In desperation I have tried to call the 2 functions from another, eg. function xyz(){test_example();testindex(str);}, but this doesn't work either.

I need this to be a single function because I want to call it from a SELECT field on a form.

This might be something simple and stupid that I've overlooked but I have tried and tried and can't find the solution.

Hoping one of you JS experts can help me on this
js_amnesiac
 
Posts: 1
Joined: Thu Apr 09, 2009 9:08 am

Who is online

Users browsing this forum: psbot [Picsearch] and 5 guests