JavaScript retain value of selected option even when refreshed retain selected value of dropdown in php,retain selected value

I am trying to get the value of a dropdown selected and pass the variable to javascript, that is working but aswell as that i am trying to save the value of a selected option even when the page is refreshed

i thought i could maybe create a cookie on a selected value and check if it exists.

any help would be appreciated

form

<form name="dummyForm">
    <select name="selectBox" onchange="getSelectedvalue()">
        <option value="option 1">option 1</option>
        <option value="option 2">option 2</option>
        <option value="option 3">option 3</option>
        <option value="option 4">option 4</option>
        <option value="option 5">option 5</option>
        <option value="option 6">option 6</option>
        <option value="option 7">option 7</option>
        <option value="option 8">option 8</option>
    </select>
</form>
<a target='_blank' href="javascript:setSelectBoxvalue()">Click me</a>

javascript

<script type="text/javascript">
function getSelectedvalue()
{
    var getSelectedIndex = document.dummyForm.selectBox.selectedIndex;
    var getSelectedOptionText = document.dummyForm.selectBox[getSelectedIndex].text;
    alert(getSelectedOptionText);
}

function setSelectBoxvalue()
{
    var len = document.dummyForm.selectBox.length;
    //alert(len);
    var optionText= 'option 1';
    for(var i=0; i<=len; i++){
        if( (document.dummyForm.selectBox[i].value) == optionText){
            alert(document.dummyForm.selectBox[i].text);
            document.dummyForm.selectBox.selectedIndex = i;
        }
    }

}
</script>
Answer:1

You can easily manipulate cookies using the functions provided by the ever useful blog QuirksMode:

http://www.quirksmode.org/js/cookies.html

Here is how to implement your specific case (the example assums you assign an ID to the select of 'selectBox'):

window.onload = function()
{
    var cookieVal = readCookie('selectBoxValue');
    if(cookieVal!=null)
    {
        document.getElementById('selectBox').selectedIndex = cookieVal;
    }
}



function changeSelectedvalue()
{
    var mySelectedIndex = document.getElementById('selectBox').selectedIndex;
    createCookie("selectBoxValue",mySelectedIndex,1000);
}



function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}

See the fiddle here:

http://jsfiddle.net/SpAm/9VmQD/

And the full screen version here:

http://jsfiddle.net/SpAm/9VmQD/embedded/result/

Answer:2

I want to obtain the remote client ip by javascript, not querying any external ip resource pools. how could i get the remote ip address, just like the way i get appName by navigator.appName also ...

I want to obtain the remote client ip by javascript, not querying any external ip resource pools. how could i get the remote ip address, just like the way i get appName by navigator.appName also ...

  1. browser games
  2. browserstack
  3. browser history
  4. browser definition
  5. browser settings
  6. browser extension
  7. browser market share
  8. browser cache
  9. browserify
  10. browserquest
  11. browserling
  12. browser cookies
  13. browser mmorpg
  14. browser pets
  15. browser vpn
  16. browser update
  17. browser fps games
  18. browser multiplayer games
  19. browser list
  20. browser for roku

Is there any simple way how to create undo redo function in Kineticjs ? I have found a Undo Manager for HTML 5 in https://github.com/ArthurClemens/Javascript-Undo-Manager, but I don't know how to put ...

Is there any simple way how to create undo redo function in Kineticjs ? I have found a Undo Manager for HTML 5 in https://github.com/ArthurClemens/Javascript-Undo-Manager, but I don't know how to put ...

  1. undo change redo

I'm attempting to create a custom popup while using the angular-leaflet-directive. I'm opening the popup from the leaflet.draw on:create event. Here: map.on('draw:created', function(e) { layer =...

I'm attempting to create a custom popup while using the angular-leaflet-directive. I'm opening the popup from the leaflet.draw on:create event. Here: map.on('draw:created', function(e) { layer =...

  1. angular-leaflet-directive
  2. angular-leaflet-directive.min.js
  3. angular-leaflet-directive npm
  4. angular leaflet directive examples
  5. angular-leaflet-directive.min.js cdn
  6. angular-leaflet-directive search
  7. angular-leaflet-directive.min.js download
  8. angular-leaflet-directive markercluster
  9. angular-leaflet-directive custom controls
  10. angular-leaflet-directive multiple maps
  11. angular-leaflet-directive.js download
  12. angular-leaflet-directive _dev_ mapped.js
  13. angular-leaflet-directive marker icon
  14. angular-leaflet-directive multiple geojson
  15. angular-leaflet-directive install
  16. angular-leaflet-directive filter markers
  17. angular-leaflet-directive draw
  18. angular-leaflet-directive popup width
  19. angular-leaflet-directive latest version
  20. angular-leaflet-directive resize

I'm from a C# Class(), fields, properties, namespace...world. And I just launched myself into javascript. So far I've been doing great. However one of my friends was looking at my code and asked me ...

I'm from a C# Class(), fields, properties, namespace...world. And I just launched myself into javascript. So far I've been doing great. However one of my friends was looking at my code and asked me ...

  1. what global variable is
  2. what is global variable in c
  3. what is global variable in java
  4. what is global variable in python
  5. what is global variable in php
  6. what is global variable in javascript
  7. what is global variable in c with example
  8. what is global variable in oracle
  9. what is global variable in matlab
  10. what is global variable in postman
  11. what is global variable with example
  12. what is global variable in sql
  13. what is global variable in talend
  14. what is global variable in salesforce
  15. what is global variable in sql server
  16. what is global variable in vba
  17. what is global variable in pl sql
  18. what is global variable in java with example
  19. what is global variable in solidworks
  20. what is global variable in js