user
user

Reputation: 1001

how to call a nested function from a html as a Onclick function

I am coding a project in the Phone Gap domain.I have to pass values from one HTML page to the other.i have done that using the following code.

 var searchString = window.location.search.substring(1),i, val, params = searchString.split("&");
    for (i=0;i<params.length;i++) 
    {
       val = params[i].split("=");
       if (val[0] == paramName) 
       {
        return unescape(val[1]);
       }
    }

i have used the value val[1] in the following format.

document.getElementById("desc").innerHTML  = results.rows.item(val[1]).desc;

now i have to images,when i click them it should go to a separate page.

so,i called the function in the following manner.

<div id="abc"> <img src="img/abc.png"  alt=""  onclick="abc()"/> </div>

i get an error

TypeError: Result of expression 'abc' [[object HTMLDivElement]] is not a function. at file:///android_asset/www/desc.html?var%20id=0:141

The entire function flow looks like this

    func A()
   {  
    Console.log("processsinggg parameter loop ");
    var searchString = window.location.search.substring(1),i, val, params = searchString.split("&");
    for (i=0;i<params.length;i++) 
     {
       val = params[i].split("=");
     if (val[0] == paramName) 
     {
       return unescape(val[1]);
     }
           console.log("valueeesss: "+val[1]);
    }
        document.getElementById("desc").innerHTML  = results.rows.item(val[1]).desc;

     function abc() 
     {
          console.log("processing abc!!");
          window.location.href=href='abc.html?var id=results.rows.item(val[1]).id';
     }
}

Upvotes: 0

Views: 905

Answers (1)

Rahul Kadukar
Rahul Kadukar

Reputation: 958

A very basic and simple solution using a JSON object to store all your data as key value pairs.The key here is the name of your ID and the value is the actual contents associated with your ID

<!DOCTYPE html>
<html>
 <head>
  <script type="text/javascript">
    var url = window.location.search.substring(1).split("&");
    var json_object = {};
    for (var i=0; i<url.length; i++) {
        var val = url[i].split("=");
        if (val[0]) 
            json_object[val[0]] = unescape(val[1]);
    }
    console.log(json_object);
   </script>
  </head>
</html>

At this point if you check the value inside json_object in your console you will see something like this

Object {param1: "132", param2: "3221"}

Here param1 is the name of your ID and 132 is the value similarly param2 is the name of your second ID and 3221 is the value. To reference these in your code. Suppose you want to assign the value of ssds to the variable param2_value you will use something like

var param2_value = json_object.param2; 

Upvotes: 1

Related Questions