0

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?

1 Answer 1

3
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";
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.