www.webdeveloper.com
Results 1 to 4 of 4

Thread: Break Commands in Functions

  1. #1
    Join Date
    Nov 2010
    Posts
    21

    Break Commands in Functions

    I had to do a break function and scripts in this portion. I am wondering if my format is correct

    here is what i made

    Code:
         function createBar(partyType,percent){ // script element to create blank cells
    switch(partyType) {
    case "D": document.write("<td class='dem'></td>");
    break;
    case "E": document.write("<td class='rep'></td>");
    break;
    case "I": document.write("<td class='ind'></td>");
    break;
    case "G": document.write("<td class='green'></td>");
    break;
    case "L": document.write("<td class='lib'></td>");
    break;
    }
    var barTxt = partyType
    
    for (i=0; i < percent; i++)
    {
    document.write(barText);
    }
    Let me know if this makes sense as I am still learning.

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Not a good approach from the beginning. It is not about the switch/case statement, it is about the document.write() method. document.write() is not a dynamic method. It is, in fact, one of the few ones which works in run time, but it is a static method. Moreover: document.write() will overwrite the document each time when it is called within a function,

    I guess you must have learned a server-side language before, so that you tend to code in the same manner in JavaScript as well. JavaScript is different: it is a client-side dynamic language language. JavaScript was meant to create/change/remove the elements of a web document, their attributes or attributes' values. In order to do that, JavaScript uses methods. Methods are nested in functions. In JavaScript functions are treated like first-class objects. Functions are triggered by events, following the user's actions.


    Regarding your problem: to create/append new elements and their attributes you should use DOM methods:

    http://www.howtocreate.co.uk/tutoria...t/domstructure
    http://www.w3schools.com/htmldom/dom_methods.asp

  3. #3
    Join Date
    Nov 2010
    Posts
    21
    ok so what would be the proper way to write this? then?

  4. #4
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by worldtraveller View Post
    ok so what would be the proper way to write this? then?
    Follow those links I have posted and you'll find the answer

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles