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?
Brian Tompsett - 汤莱恩
5,92772 gold badges64 silver badges135 bronze badges
asked Sep 9, 2011 at 23:40
Philip Kirkbride
23.2k39 gold badges134 silver badges238 bronze badges
1 Answer 1
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";
}
answered Sep 9, 2011 at 23:42
Joseph Marikle
78.8k18 gold badges114 silver badges130 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-js