Mansi Desai
Mansi Desai

Reputation: 227

How to pass multiple parameter in javascript function which is called from another javascript function?

I am using google maps and the infowindow that opens on the click of the marker calls a JavaScript function.
There is another function with multiple parameters that has to be called from this function.

The following is my code:

function createMarker(point,custid,streetadd,city,state,zip,address,phone,website,co) {         
    var infowindowHover,infowindowClick;

    var hoverText = "<CENTER><B>" + co + "</B></CENTER>";
    var markerMarkup = "<TABLE><TR><TD colspan='2'><B>";
    markerMarkup = markerMarkup + co + "</B></TD></TR><TR><TD colspan='2'>";
    markerMarkup = markerMarkup + streetadd + "</TD></TR><TR><TD colspan='2'>";
    markerMarkup = markerMarkup + city + "," + state + " " + zip + "</TD></TR><TR><TD colspan='2'>";
    markerMarkup = markerMarkup + phone + "</TD></TR><TR><TD colspan='2'>";
    if(website.indexOf("http://")>0) {
        markerMarkup = markerMarkup +"<a href=";
    } else {
        markerMarkup = markerMarkup +"<a href=http://";
    }
    markerMarkup = markerMarkup + website + " target=_blank>" + website + "</a></TD></TR><TR><TD>";                                
    var funCall = custid + "," + streetadd + "," + city + "," + state + "," + zip + "," + address + "," + phone + "," + website + "," + co;
    markerMarkup = markerMarkup + "<input type='button' class='button' value='see available styles' id='styles' onclick='setstyles("+ funCall +");'></input>";
    //markerMarkup = markerMarkup + "<input type='button' class='button' value='see available styles' id='styles' onclick='setstyles("+ custid +");'></input>"; 

    markerMarkup = markerMarkup + "</TD></TR></TABLE>";

    var marker = new google.maps.Marker({
        position: point,
        map: map,
        icon: image             
    });

    google.maps.event.addListener(marker, "mouseover", function () {
        if (infowindowHover) infowindowHover.close();
        infowindowHover = new google.maps.InfoWindow({content: hoverText});
        infowindowHover.open(map, marker);
    });

    google.maps.event.addListener(marker, "mouseout", function () {
        if (infowindowHover) infowindowHover.close();                
    });  

    google.maps.event.addListener(marker, "click", function () {
        if (infowindowClick) infowindowClick.close();
        infowindowClick = new google.maps.InfoWindow({ content: markerMarkup });
        infowindowClick.open(map, marker);
    });

    //google.maps.event.addListener(marker, "mouseout", function () {
    //    if (infowindowClick) infowindowClick.close();                
    //});
    return marker;
}

The second function that I want to call from this function is:

function setstyles(idcust,streetadd,city,state,zip,address,phone,website,co){
    var msg= "This feature is  available only to logged-in ";    
    alert(idcust);                     
    <%if Session("ctype")="1" then %>
        alert(msg + " non wholesalers.");
    <%else %>           
        <%if Session("ctype")="0" then %>
            var storestyles = 'storestyles.asp?id=' + idcust;         
            document.getElementById('storeaddresses').style.display = 'block';             
        <%else %>
             alert(msg + "users."); 
        <% end if %>
    <%end if %>
}

I want to pass the parameters from the createMarker() to the setstyles() which currently I am unable to do.

Upvotes: 0

Views: 2491

Answers (2)

rdiazv
rdiazv

Reputation: 1153

In this line

var funCall = custid + "," + streetadd + "," + city + "," + state + "," + zip + "," + address + "," + phone + "," + website + "," + co;

If any of the parameters it's a string, you should be wrapping them into quotes.

For example, if city it's expected to be a string:

var funCall = custid + "," + streetadd + ",\"" + city + "\"," + state + "," + zip + "," + address + "," + phone + "," + website + "," + co;

Upvotes: 2

nozzleman
nozzleman

Reputation: 9649

In this line:

    var funCall = custid + "," + streetadd + "," + city + "," + state + "," + zip + "," + address + "," + phone + "," + website + "," + co;

you simply build one huge string containing the variables separated by commas. So in this case you also commit only one parameter (the string). Try wrapping all the params into a parameter-object like that:

     var params = {
        custID: custid,
        streetAdd: streetadd,
        [...]
     }

and commit that to the function at onClick="setStyles(params)"...

The signature of setStyles should look like this: function setStyles(params){...}

Within setStyles you can access each parameter using sth. like params.custID or whatever you called the members of you parameter-object like....

Also consider the hints given in the comments below you question like using += which is considered good coding-style...

Upvotes: 1

Related Questions