Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Some Help?

  1. #1
    Join Date
    Sep 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrow Some Help?

    I am a PHP guy, Javascript confuses me to an extreme.

    I was wondering if anyone knew a way where I could do the following:

    Have multiple chained select forms, the values of which are combined and displayed.

    I'm trying to use Javascript to change the style of text based on the items selected. So if someone had the color red, and a border selected, the script would dynamically change the style of some text to display that.

    Sorry if this sounds a bit confusing, it's hard for me to explain. If anyone could be of any help it would be much appreciated.

    Thanks

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    CSS is the easiest way to do this.
    HTML Code:
    <script type="text/javascript">
    var e = document.getElementById('changeable').style;
    </script>
    Font weight:
    <select onchange="e.fontWeight = this.value;">
      <option value="lighter">Lighter</option>
      <option value="normal">Normal</option>
      <option value="bold">Bold</option>
      <option value="bolder">Bolder</option>
    </select>
    Colour: <!-- I'm British, before you ask -->
    <select onchange="e.color = this.value;">
      <option value="red">Red</option>
      <option value="green">Green</option>
      <option value="blue">Blue</option>
    </select>
    Background colour:
    <select onchange="e.backgroundColor = this.value;">
      <option value="red">Red</option>
      <option value="green">Green</option>
      <option value="blue">Blue</option>
    </select>
    Text decoration:
    <select onchange="e.textDecoration = this.value;">
      <option value="underline">Underline</option>
      <option value="overline">Overline</option>
      <option value="line-through">Strikethrough</option>
      <option value="none">None</option>
    </select>
    <p id="changeable">
    Blah de blah.
    </p>
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    That's ingenious! But, to work, the script needs to be at the end or executed onload. It would also be a nice touch to set the initial style properties of the element (paragraph) to the initial selections.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Quote Originally Posted by John
    That's ingenious!
    Why thank you!
    But, to work, the script needs to be at the end or executed onload.
    I don't quite understand. The script (if you mean the <script> element [all one line of it]) should be executed when the browser parses it. This is perfectly fine for the purposes. Am I missing something?
    It would also be a nice touch to set the initial style properties of the element (paragraph) to the initial selections.
    You're right. Add:
    Code:
    <script type="text/javascript">
    var i, s=document.getElementsByTagName("select");
    for(i=0;i<s.length;i++) if(s[i].onchange.indexOf("e.") > -1) s[i].onchange();
    </script>
    to the end of the script.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by me
    But, to work, the script needs to be at the end or executed onload.
    Quote Originally Posted by Twey
    I don't quite understand. The script (if you mean the <script> element [all one line of it]) should be executed when the browser parses it. This is perfectly fine for the purposes. Am I missing something?
    Well, I tried it and the script (all one line of it) needed to be after the element it references, at least. I cannot understand your code for setting the initial properties but will give it a shot. I was playing around with the script and settled on just adding the selected attribute to the options that described the default state of the text, adding options if needed. It is a fine point of the select element but, you are supposed to designate a selected option anyway because some browsers will default select differently from one another otherwise, unless this is unimportant to the page's design.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I understand the additions now but needed to convert toString() before evaluating:
    Code:
    <script type="text/javascript">
    <!--
    var e = document.getElementById('changeable').style;
    var i, s=document.getElementsByTagName("select");
    for(i=0;i<s.length;i++)
    if(s[i].onchange.toString().indexOf("e.") > -1)
    s[i].onchange();
    //-->
    </script>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1
    I understand the additions now but needed to convert toString() before evaluating:
    Checking for the occurance of 'e.' is far from a robust solution, particularly when you consider that something as common as '.style.' would match.

    Checking the class attribute is the most common solution:

    Code:
    if(/(^|\s+)...(\s+|$)/.test(s[i].className)) {
    replacing the ellipsis with whatever class name you choose.

    Mike

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Agreed but, I was just making Twey's code work. He seems to have a love of short, nondescript variable names.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Oh, I see.
    Final revised code:
    HTML Code:
    <p id="changeable">
    Blah de blah.
    </p>
    Font weight:
    <select class="pchanger" onchange="document.getElementById('changeable').style.fontWeight = this.value;">
      <option value="lighter">Lighter</option>
      <option value="normal">Normal</option>
      <option value="bold">Bold</option>
      <option value="bolder">Bolder</option>
     </select>
    Colour:
    <select class="pchanger" onchange="document.getElementById('changeable').style.color = this.value;">
      <option value="red">Red</option>
      <option value="green">Green</option>
      <option value="blue">Blue</option>
    </select>
    Background colour:
    <select class="pchanger" onchange="document.getElementById('changeable').style.backgroundColor = this.value;">
      <option value="red">Red</option>
      <option value="green">Green</option>
      <option value="blue">Blue</option>
    </select>
    Text decoration:
    <select class="pchanger" onchange="document.getElementById('changeable').style.textDecoration = this.value;">
      <option value="underline">Underline</option>
      <option value="overline">Overline</option>
      <option value="line-through">Strikethrough</option>
      <option value="none">None</option>
    </select>
    <script type="text/javascript">
    var i, s=document.getElementsByTagName("select");
    for(i=0;i<s.length;i++) if(/(^|\s+)pchanger(\s+|$)/.test(s[i].className)) s[i].onchange();
    </script>
    /EDIT: Mike also loves short, non-descriptive variable names There's a difference between the way I'd name a real variable and the way I'd name a quick alias masquerading as a variable, especially when they're defined in the very last line. Not a long reference.
    Last edited by Twey; 09-18-2005 at 03:27 PM. Reason: Missed a keyword.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  10. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I got that but, I'm begining to wonder if you test your own code very often. This latest doesn't work either, you left 'style' out of the onchange events. Here is a working version:
    Code:
    Font weight:
    <select class="pchanger" onchange="pchange.fontWeight = this.value;">
      <option value="lighter">Lighter</option>
      <option value="normal">Normal</option>
      <option value="bold">Bold</option>
      <option value="bolder">Bolder</option>
    </select>
    Colour: <!-- I'm British, before you ask -->
    <select class="pchanger" onchange="pchange.color = this.value;">
      <option value="red">Red</option>
      <option value="green">Green</option>
      <option value="blue">Blue</option>
      <option value="black">Black</option>
    </select>
    Background colour:
    <select class="pchanger" onchange="pchange.backgroundColor = this.value;">
      <option value="white">White</option>
      <option value="red">Red</option>
      <option value="green">Green</option>
      <option value="blue">Blue</option>
    </select>
    Text decoration:
    <select class="pchanger" onchange="pchange.textDecoration = this.value;">
      <option value="underline">Underline</option>
      <option value="overline">Overline</option>
      <option value="line-through">Strikethrough</option>
      <option value="none">None</option>
    </select>
    <p id="changeable">
    Blah de blah.
    </p>
    <script type="text/javascript">
    <!--
    var pchange = document.getElementById('changeable').style;
    var i, s=document.getElementsByTagName("select");
    for(i=0;i<s.length;i++)
    if(s[i].className=='pchanger')
    s[i].onchange();
    //-->
    </script>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

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