SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2005
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    js redirect on select

    hello, i have a select box that is populated with countries. When the user selects a county i would the item to be appended to the url then redirected, so if the user selects "America" the url is appended and redirected to page.php?county=America. How can i do this?

    thanks

  2. #2
    SitePoint Zealot loathsome's Avatar
    Join Date
    Jul 2008
    Location
    Norway
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could use the onclick-attribute. For example
    Code JavaScript:
    <div onclick="window.location='?country=America';return false"></div>
    Though, I recommend you use events, but this is quite troublesome unless you're using a full fledged library like Prototype or jQuery.

    Time you enjoy wasting is not wasted time.

    Loathsome Systems

  3. #3
    SitePoint Evangelist
    Join Date
    Mar 2005
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by loathsome View Post
    You could use the onclick-attribute. For example
    Code JavaScript:
    <div onclick="window.location='?country=America';return false"></div>
    Though, I recommend you use events, but this is quite troublesome unless you're using a full fledged library like Prototype or jQuery.

    thanks. would that go in the select or the option? if it goes in the select, how can i append the value of the selected option to the href?

  4. #4
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code JavaScript:
    document.getElementById('Put Select-Box ID here').onchange = function() {
        var value = this.getElementsByTagName('option')[this.selectedIndex].value;
        window.location = 'page.php?country='+value;
    }
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  5. #5
    SitePoint Evangelist
    Join Date
    Mar 2005
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that's nice - does it need to be in a function though? I have several select boxes that the same append/redirect aspect will need to apply to?

  6. #6
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Define the ID of each select box in an array then and then loop through them... -

    Code JavaScript:
    var boxes = new Array('id1','id2','id3','etc...');
     
    for( var i = 0 ; i < boxes.length ; i++ ){
        document.getElementById(boxes[i]).onchange = function() {
            var value = this.getElementsByTagName('option')[this.selectedIndex].value;
            window.location = 'page.php?country='+value;
        }
    }
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)


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
  •