Having trouble at work when trying to enhance a function. I just took an introductory couse in Javascript so I'm hoping I will find a lot of people to help me get better. :)
here is an example of what I want to do:
<script type="text/javascript">
var title1 = "<h2>Cisco</h2>";
var summary1 = "<p>Cool class</p>";
var Register1 = "<button>Register</button>";
var title2 = "<h2>Whatever title</h2>";
var summary2 = "<p>Whatever Summary</p>";
var Register2 = "<button>Whatever Register</button>";
//But I have to create about 10 functions if I do it this way...
function course1()
{
document.getElementById('courseTitle').innerHTML = title1;
document.getElementById('courseSummary').innerHTML = summary1;
document.getElementById('courseRegister').innerHTML = Register1;
}
function course2()
{
document.getElementById('courseTitle').innerHTML = title2;
document.getElementById('courseSummary').innerHTML = summary2;
document.getElementById('courseRegister').innerHTML = Register2;
}
//So I taught about doing it with parameters so I could only build one function.
//For some reason it doesn't work.
//Here is how my improved function idea looks
function course(x)
{
document.getElementById('courseTitle').innerHTML = title+x;
document.getElementById('courseSummary').innerHTML = summary+x;
document.getElementById('courseRegister').innerHTML = Register+x;
}
//And here is how you would call them
<input type"button" value="course1" onclick="course(1)">
<input type"button" value="course1" onclick="course(2)">
I would appreciate any help! Thank You.
You could use an array to hold the data:
var data = [{
title: "<h2>Cisco</h2>",
summary: "<p>Cool class</p>",
Register: = "<button>Register</button>"
},{
title: "<h2>Whatever title</h2>",
summary: "<p>Whatever Summary</p>",
Register: "<button>Whatever Register</button>"
}];
and then just pass an index to your function:
function course( index ) {
document.getElementById('courseTitle').innerHTML = data[index].title;
document.getElementById('courseSummary').innerHTML = data[index].summary;
document.getElementById('courseRegister').innerHTML = data[index].Register;
}
you need to use an Array
var globalCourses = [['cisco','cool class','register'],['whatever','whatever sum','whatever reg']];
function course(i){
var title = globalCourses[i][0];
var summary = globalCourses[i][1];
var register = globalCourses[i][2];
//do what you need with this
document.getElementById('courseTitle').innerHTML = title;
//etc
}
so anytime you think of a set of data whose parameters are the same, use arrays or arrays of objects.
you could also do something like:
var globalCourses = [{title:"title",summary:"sum",register:"register"},{title:"another title",summary:"another sum",register:"another register"}];
function course(i){
var title = globalCourses[i].title;
var summary = globalCourses[i].summary;
//etc
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.