+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 20

Thread: Javascript/DHTML help

  1. #1
    callumacrae's Avatar
    callumacrae is offline not alex mac callumacrae is just really nice
    Join Date
    Dec 2007
    Location
    Wellesbourne, England
    Posts
    5,162

    Javascript/DHTML help

    Helloo,

    I'm working on a script ( http://paperhub.x10hosting.com/sigbar ) but I am having trouble with some DHTML. The code so far:

    HTML Code:
    <p><a class="colour" id="black" onMouseOver="createcolour('black')" style="font-weight:bold" onclick="bold('black')">Black</a>
       <a class="colour" id="red" onMouseOver="createcolour('red')" onclick="bold('red')">Red</a>
       <a class="colour" id="orange" onMouseOver="createcolour('orange')" onclick="bold('orange')">Orange</a>
       <a class="colour" id="yellow" onMouseOver="createcolour('yellow')" onclick="bold('yellow')">Yellow</a>
       <a class="colour" id="green" onMouseOver="createcolour('green')" onclick="bold('green')" weight="bold">Green</a>
       <a class="colour" id="blue" onMouseOver="createcolour('blue')" onclick="bold('blue')">Blue</a>
       <a class="colour" id="purple" onMouseOver="createcolour('purple')" onclick="bold('purple')">Purple</a></p>
       <img src="original/black.jpg" id="previewcolour" alt="" />
    Code:
         function createcolour(colour){
         document.getElementById('previewcolour').src = 'original/'+colour+'.jpg';
         document.getElementById('previewstyle').src = 'original/'+colour+'.jpg';
         }
    
         function bold(boldcolour){
    alert(boldcolour);
         document.getElementByClass('colour').font-weight = 'normal';
         document.getElementById(boldcolour).font-weight = 'bold';
         
         }
    The second bit of js code obviously doesn't work, but it makes it easier to understand what I'm trying to do. How would I make it work?

    ~Callum
    Edit:
    The first bit of javascript isn't working either any more :/

    ~Callum
    Last edited by callumacrae; 01-07-2010 at 07:33 AM. Reason: Automerged Doublepost
    I can customise your phpBB board. Send me a PM.
    lynxphp - info, tutorials and scripts
    "A forum post should be like a skirt; long enough to cover the subject but short enough to keep things interesting."

  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Javascript/DHTML help

    Check your error console. "-" isn't a valid character in identifiers. The "-" in bold() is a subtraction operator. Style property names in JS are formed by removing the "-" from the CSS name and capitalizing the following letter (that is, they use CamelCase). Use "fontWeight", not "font-weight".

    Also, there's no such function as getElementByClass. Some browsers support getElementsByClassName, but not IE. You'll have to test whether it's defined and, if not, define it. It should return a collection of nodes, not a single node.

    Code:
    function bold(boldcolour){
        var colours = document.getElementsByClassName('colour');
        for (var i=0; i<colours.length; ++i) {
            colours[i].style.fontWeight = 'normal';
        }
        document.getElementById(boldcolour).style.fontWeight = 'bold'; 
     }
    Last edited by misson; 01-07-2010 at 08:11 AM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  3. #3
    slacker3 is offline x10 Sophmore slacker3 is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    146

    Re: Javascript/DHTML help

    did you forget ".style" ?

    e.g.
    document.getElementById("menu").style.visibility = "hidden";

  4. #4
    callumacrae's Avatar
    callumacrae is offline not alex mac callumacrae is just really nice
    Join Date
    Dec 2007
    Location
    Wellesbourne, England
    Posts
    5,162

    Re: Javascript/DHTML help

    Thanks

    Fixed that, but the functions aren't getting called any more :/

    ~Callum
    I can customise your phpBB board. Send me a PM.
    lynxphp - info, tutorials and scripts
    "A forum post should be like a skirt; long enough to cover the subject but short enough to keep things interesting."

  5. #5
    callumacrae's Avatar
    callumacrae is offline not alex mac callumacrae is just really nice
    Join Date
    Dec 2007
    Location
    Wellesbourne, England
    Posts
    5,162

    Re: Javascript/DHTML help

    No, I've got .style now. Updated code:

    Code:
         function createcolour(colour){
    alert(colour);
         document.getElementById('previewcolour').src = 'original/'+colour+'.jpg';
         document.getElementById('previewstyle').src = 'original/'+colour+'.jpg';
         }
    
         function bold(boldcolour){
    alert(boldcolour);
         document.getElementByClass('colour').style.fontWeight = 'normal';
         document.getElementById(boldcolour).style.fontWeight = 'bold';
         
         }
    ~Callum
    Last edited by callumacrae; 01-07-2010 at 07:57 AM. Reason: typo
    I can customise your phpBB board. Send me a PM.
    lynxphp - info, tutorials and scripts
    "A forum post should be like a skirt; long enough to cover the subject but short enough to keep things interesting."

  6. #6
    marshian's Avatar
    marshian is offline x10 Elder marshian is an unknown quantity at this point
    Join Date
    Jan 2008
    Location
    Belgium
    Posts
    526

    Re: Javascript/DHTML help

    About function bold():
    Check out this page: http://www.w3schools.com/Css/pr_font_weight.asp
    Instead of font-weight, it should be fontWeight.

    Secondly, getElementByClass doesn't exist. There is no actual function build-in to do this afaik, but you can write one yourself quite easily. You can use getElementsByTagName (see http://www.w3schools.com/jsref/dom_obj_document.asp) loop over them, check their className (http://www.w3schools.com/jsref/dom_obj_object.asp) and finally return an array containing all objects with that tag name and class name.

    The first appears correct though, see http://www.w3schools.com/jsref/dom_obj_image.asp :dunno:
    Real programmers don't document their code - if it was hard to write, it should be hard to understand.

  7. #7
    callumacrae's Avatar
    callumacrae is offline not alex mac callumacrae is just really nice
    Join Date
    Dec 2007
    Location
    Wellesbourne, England
    Posts
    5,162

    Re: Javascript/DHTML help

    Thanks. They're not being called though.

    ~Callum
    Edit:
    Ooh they are now

    ~Callum
    Last edited by callumacrae; 01-07-2010 at 08:07 AM. Reason: Automerged Doublepost
    I can customise your phpBB board. Send me a PM.
    lynxphp - info, tutorials and scripts
    "A forum post should be like a skirt; long enough to cover the subject but short enough to keep things interesting."

  8. #8
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Javascript/DHTML help

    See my other, updated reply for other problems. Note that you don't need to use getElementsByClassName:

    Code:
    var bold = (function () {
        var currElt = {style: {}};
        return function(boldColour) {
            currElt.style.fontWeight = '';
            currElt = document.getElementById(boldColour);
            currElt.style.fontWeight = 'bold';
        }
    })();
    Last edited by misson; 01-07-2010 at 08:09 AM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  9. #9
    callumacrae's Avatar
    callumacrae is offline not alex mac callumacrae is just really nice
    Join Date
    Dec 2007
    Location
    Wellesbourne, England
    Posts
    5,162

    Re: Javascript/DHTML help

    I've just written getElementById lots of times

    How do I get a variable to carry over between functions?

    Code:
         function createcolour(colour){
    //alert(colour);
         document.getElementById('previewcolour').src = 'original/'+colour+'.jpg';
         }
    
         function bold(boldcolour){
    //alert(boldcolour+'ww');
         document.getElementById('black').style.fontWeight = 'normal';
         document.getElementById('red').style.fontWeight = 'normal';
         document.getElementById('orange').style.fontWeight = 'normal';
         document.getElementById('yellow').style.fontWeight = 'normal';
         document.getElementById('green').style.fontWeight = 'normal';
         document.getElementById('blue').style.fontWeight = 'normal';
         document.getElementById('purple').style.fontWeight = 'normal';
    
         document.getElementById(boldcolour).style.fontWeight = 'bold';
         document.getElementById('previewstyle').src = 'original/'+boldcolour+'.jpg';
         var colour=boldcolour
         }
    
    
         function createstyle(style){
    alert(colour);
         //document.getElementById('previewstyle').src = style+'/'+colour+'.jpg';
         }
    I want the variable "boldcolour" in the second function to be "colour" in the third function.

    ~Callum
    Edit:
    Here is the updated code:

    Index.php:

    HTML Code:
    <p><a class="colour" id="black" onMouseOver="createcolour('black')" style="font-weight:bold" onclick="bold('black')">Black</a> - 
       <a class="colour" id="red" onMouseOver="createcolour('red')" onclick="bold('red')">Red</a> - 
       <a class="colour" id="orange" onMouseOver="createcolour('orange')" onclick="bold('orange')">Orange</a> - 
       <a class="colour" id="yellow" onMouseOver="createcolour('yellow')" onclick="bold('yellow')">Yellow</a> - 
       <a class="colour" id="green" onMouseOver="createcolour('green')" onclick="bold('green')" weight="bold">Green</a> - 
       <a class="colour" id="blue" onMouseOver="createcolour('blue')" onclick="bold('blue')">Blue</a> - 
       <a class="colour" id="purple" onMouseOver="createcolour('purple')" onclick="bold('purple')">Purple</a></p>
       <img src="original/black.jpg" id="previewcolour" alt="" />
    
    <?php endBox() ?>
    <br />
    <?php startBox("Step Two: Choose style"); ?>
    
    <p><a id="original" onMouseOver="createstyle('original')" onclick="select('original')" style="font-weight:bold">Original</a> - 
       <a id="deepCut" onMouseOver="createstyle('deepCut')" onclick="select('deepCut')">Deep Cut</a> - 
       <a id="sineMachine" onMouseOver="createstyle('sineMachine')" onclick="select('sineMachine')">Sine Machine</a></p>
       <img src="original/black.jpg" id="previewstyle" alt="" />
    
    <?php endBox() ?>
    <br />
    <?php startBox("Step Three: Enter text"); ?>
    <form action="image.php" method="get" id="makeown">
        <table style="width:800px; border:0px white solid">
         <tr>
          <td>
           <table style="width:100%; border:0px white solid">
            <tr>
             <td style="text-align:right"><b>Name</b></td>
             <td><input type="text" size="50" name="name" id="name" style="font-family:'Courier New', Courier, monospace" /></td>
            </tr>
            <tr>
             <td style="text-align:right"><b>Line 1 Text</b></td>
             <td><input type="text" size="50" name="line1" id="line1" style="font-family:'Courier New', Courier, monospace" /></td>
            </tr>
            <tr>
             <td style="text-align:right"><b>Line 2 Text</b></td>
             <td><input type="text" size="50" name="line2" id="line2" style="font-family:'Courier New', Courier, monospace" /></td>
            </tr>
           <tr>
            <td style="text-align:right">&nbsp;</td>
            <td><input type="button" value="Make Image" onclick="process()" /></td>
           </tr>
          </table>
         </form>
        </td>
       </tr>
       <tr>
        <td><img src="image.php?name=NAME&line1=Line1&line2=Line2" style="width:369px; height:30px" alt="Example Signature Bar" id="previewImage" /></td>
       </tr>
      </table>
    
    <?php endBox() ?>
    <br />
    <?php startBox("Step Four: Copy into your signature"); ?>
    
    <p>Now you can copy and paste your sigbar into a forum, blog or your website.</p>
          <table>
           <tr>
            <td style="text-align:right"><b>HTML (for websites)</b></td>
            <td><input type="text" name="urlhtml" id="url" size="50" /></td>
           </tr>
           <tr>
            <td style="text-align:right"><b>BB Code (for most forums)</b></td>
            <td><input type="text" name="urlbb" id="urb" size="50" /></td>
           </tr>
          </table>

    script.js:
    Code:
         function createcolour(colour){
         document.getElementById('previewcolour').src = 'original/'+colour+'.jpg';
         }
    
         function bold(boldcolour){
         document.getElementById('black').style.fontWeight = 'normal';
         document.getElementById('red').style.fontWeight = 'normal';
         document.getElementById('orange').style.fontWeight = 'normal';
         document.getElementById('yellow').style.fontWeight = 'normal';
         document.getElementById('green').style.fontWeight = 'normal';
         document.getElementById('blue').style.fontWeight = 'normal';
         document.getElementById('purple').style.fontWeight = 'normal';
    
         document.getElementById(boldcolour).style.fontWeight = 'bold';
         document.getElementById('previewstyle').src = 'original/'+boldcolour+'.jpg';
         document.getElementById('previewtext').src = 'original/'+boldcolour+'.jpg';
    
    
         var colour=boldcolour; //doesn't work, needs to make a variable called colour for the next function
    
    
         }
    
    
         function createstyle(style){
         document.getElementById('previewstyle').src = style+'/black.jpg';
         }
    
         function select(boldstyle){
         document.getElementById('original').style.fontWeight = 'normal';
         document.getElementById('deepCut').style.fontWeight = 'normal';
         document.getElementById('sineMachine').style.fontWeight = 'normal';
    
         document.getElementById(boldstyle).style.fontWeight = 'bold';
         document.getElementById('previewImage').src = 'image.php?name=NAME&line1=Line1&line2=Line2&style='+boldstyle+'&colour='+boldcolour;
    
    
         var style=boldstyle;  //doesn't work, needs to make a variable called colour for the next function
    
    
         }
    
        function process(){
             function process(){
         var name = document.getElementById('name').value;
         var line1 = document.getElementById('line1').value;
         var line2 = document.getElementById('line2').value;
    
         var sigbarurl = 'image.php?name='+name+'&line1='+line1+'&line2='+line2+'&colour='+colour+'&style='style;
         var linkurl = 'http://paperhub.x10hosting.com/sigbar/';
    
         document.getElementById('previewImage').src = sigbarurl
    
         document.getElementById('urlhtml').value = '<a href="'+linkurl+'"><img src="' + sigbarurl + '" /></a>';
         document.getElementById('urlbb').value = '[img]' + sigbarurl + '[/img]';
    
         }
    I can't work out how to get the variables between statuses

    ~Callum
    Last edited by callumacrae; 01-07-2010 at 09:05 AM. Reason: Automerged Doublepost
    I can customise your phpBB board. Send me a PM.
    lynxphp - info, tutorials and scripts
    "A forum post should be like a skirt; long enough to cover the subject but short enough to keep things interesting."

  10. #10
    marshian's Avatar
    marshian is offline x10 Elder marshian is an unknown quantity at this point
    Join Date
    Jan 2008
    Location
    Belgium
    Posts
    526

    Re: Javascript/DHTML help

    It has to do with variables being global or not, but that's not exactly very readable, nor useful...
    I *think* something like this should work:
    Code:
    var foo;
    function one(bar) {
     foo = bar;
    }
    function two() {
     alert(foo);
    }
    Notice the "var foo" in the global scope, and no "var foo" definitions inside the function's scopes!
    Last edited by marshian; 01-07-2010 at 09:09 AM.
    Real programmers don't document their code - if it was hard to write, it should be hard to understand.

+ Reply to Thread
Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers