www.webdeveloper.com
Results 1 to 5 of 5

Thread: Can't get a javscript to work. :(

Hybrid View

  1. #1
    Join Date
    Feb 2009
    Location
    United Kingdom
    Posts
    4

    Question Can't get a javscript to work. :(

    Hi everyone,

    I'm having difficulty getting some javascript to work on my website. What I'm trying to do is get a message to appear on a web page when a drop-down list isn't on the page and to disappear when it is on the page. The drop-down only appears to some users, depending on their customer account status.

    This is what I've got so far:

    <div style="position:relative;">
    <div id="theDiv" style="position:absolute; left:0px; top:0px; z-index:2;">
    </div>
    </div>

    When the drop-down is on the page, the code appears here. <select class="sortby_select" id="SortBy">....(options)....</select>

    <script type="text/javascript">
    var writing = document.getElementById("theDiv");
    var dropdown = document.getElementById("SortBy");
    var selectedText = dropdown.options[dropdown.selectedIndex].text;
    if (selectedText == null){
    writing.innerHTML = "<p>No products here!</p>";
    writing.style.height = "300px";
    writing.style.border = "1px solid black";
    writing.style.backgroundColor = "#FFFFE0";
    writing.style.padding = "40px";
    }
    </script>

    But this doesn't work.
    If I change "if (selectedText == null)" to "if (selectedText !== null)" then the message appears on the screen when the drop-down is on the screen—the exact opposite of what I want, so I thought just removing the exclamation mark would switch it around, but no.

    Any help with this would be greatly appreciated.

    BrassMonkey

  2. #2
    Join Date
    Feb 2009
    Location
    United Kingdom
    Posts
    4
    If it helps, this is the web page: http://www.alisonnash.co.uk/Shabby-C...gns-s/1834.htm

    BrassMonkey

  3. #3
    Join Date
    Jan 2004
    Location
    chertsey, a small town s.w. of london, england.
    Posts
    1,458
    Hi there BrassMonkey,

    try it like this...
    Code:
    
    <script type="text/javascript">
    (function() {
       'use strict';
    
       var writing=document.getElementById('theDiv');
    
    if(document.getElementById('SortBy')==null) {
       writing.innerHTML='<p>No products here!<\/p>';
       writing.style.height='300px';
       writing.style.border='1px solid #000';
       writing.style.backgroundColor='#ffffe0';
       writing.style.padding='40px';
     }
    })();
    </script>
    coothead
    Last edited by coothead; 01-15-2014 at 08:11 AM.

  4. #4
    Join Date
    Feb 2009
    Location
    United Kingdom
    Posts
    4
    Hi coothead,

    That's great, it works! Thanks so much.

    BrassMonkey

  5. #5
    Join Date
    Jan 2004
    Location
    chertsey, a small town s.w. of london, england.
    Posts
    1,458

    No problem, you're very welcome.

    coothead

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