Philip Kirkbride
Philip Kirkbride

Reputation: 22879

Passing a javascript value

I want to pass a numeric value through to the following Javascript function.

function swap2() {
var oldDiv = document.getElementById("product-grid");
var newDiv = document.getElementById("product-page");
oldDiv.style.display = "none";
newDiv.style.display = "block";
}

I want to be able to call the function with a number in the bracket like...

onclick="swap2(2)"

And then have the newDiv variable change based on that number like so...

 var newDiv = document.getElementById("product-page2");

How can I go about doing this?

Upvotes: 0

Views: 62

Answers (1)

Joseph Marikle
Joseph Marikle

Reputation: 78550

function(variable){
    // process using 'variable'
}

that's how you pass a variable to a function. Thus:

function swap2(n) {
    var oldDiv = document.getElementById("product-grid");
    var newDiv = document.getElementById("product-page" + n);
    oldDiv.style.display = "none";
    newDiv.style.display = "block";
}

Upvotes: 3

Related Questions