0

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.

4
  • 2
    No, sorry, this is not an example of what you want to do. This is a sample of code without a question... Commented Dec 5, 2013 at 16:44
  • @adamb the question is inside the code AND OP, try explaining what you need in the content of the Question, not in comments in the code Commented Dec 5, 2013 at 16:53
  • I suggest to read a JavaScript tutorial to learn even more about the basics: eloquentjavascript.net. Commented Dec 5, 2013 at 17:10
  • That worked Victor Ferreira! Thanks all for the tips. I'll be more clear if a next question. This site is awesome. Commented Dec 5, 2013 at 18:15

2 Answers 2

4

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

Comments

2

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
}

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.