0

I have the following trial.html Code.It has a <select> tag. PS: It is not duplicate as it doesn't use Ajax and i am not familiar with Ajax and there are people like me out there .

<!DOCTYPE html>
<html>
<head>
  <title>Popup</title>
  <script type="text/javascript" src="jquery.min.js"></script>
  <script src="trial.js"></script>
</head>
<body>
  <h1></h1>
  <div role="main">
    <form >
      <label for="timeframe">Remove all browsing data from:</label>

      <select id="timeframe" name="timeframe">
        <option value="Null">Please Select one</option>
        <option  value="hour">the past hour</option>
        <option value="day">the past day</option>
        <option value="week">the past week</option>
        <option value="4weeks">the past four weeks</option>
        <option value="forever">the beginning of time</option>
      </select>

      <input type="submit" id="submit" value="submit">
    </form>
  </div>
</body>
</html>

This is the trial.js

   $(document).ready(
function(){
var selected;
  $("#timeframe").change(

function(){
selected=$("#timeframe option:selected").val();
})
///Figure out how to use selected outside.
//function that gives time taking the selecte as input
 parseMilliseconds= function (timeframe) {
    var now = new Date().getTime();
    var milliseconds = {
      'hour': 60 * 60 * 1000,
      'day': 24 * 60 * 60 * 1000,
      'week': 7 * 24 * 60 * 60 * 1000,
      '4weeks': 4 * 7 * 24 * 60 * 60 * 1000
    };

    if (milliseconds[timeframe])
      return now - milliseconds[timeframe];

    if (timeframe === 'forever')
      return 0;

    return null;
  },




if(selected!=undefined){
//that deletes history
 chrome.browsingData.remove(
{ "since" : parseMilliseconds(selected) }, {
        "appcache": true,
        "cache": true,
        "cookies": true,
        "downloads": true,
        "fileSystems": true,
        "formData": true,
        "history": true,
        "indexedDB": true,
        "localStorage": true,
        "serverBoundCertificates": true,
        "pluginData": true,
        "passwords": true,
        "webSQL": true
      }


  , function callback(){
 alert("Data Deleted");

  })

}



}
)

SO I wanted to use the variable selected outside the callback function on the change event . How do i do it?

Eminem347
  • 387
  • 4
  • 11
  • 1
    Well, the question is more like, what is a man trying to achieve? A man can use $("#timeframe option:selected").val() at any time to get the value. – Lain Dec 02 '16 at 20:48
  • you have a syntax error at `id="submit" value="submit" ">` with one `"` too much – Endless Dec 02 '16 at 20:51
  • @Latin I wanted to first select the duration and hence it must be passed through the change event handler of $('#timeframe') – Eminem347 Dec 02 '16 at 20:51
  • @Endless Thanks so much :) still I am not able to access it. – Eminem347 Dec 02 '16 at 20:51
  • Set the var selected before/outside the $(document) part so it becomes global. Yet, I still do not get the use of it. – Lain Dec 02 '16 at 20:57
  • @Lain I am uploading the full code – Eminem347 Dec 02 '16 at 20:59
  • Maybe try `window.selected = ...`? – Endless Dec 02 '16 at 21:00
  • @Endless I am a beginner so I vary little idea about the window model :( ,am working and learning part by part....can you tell me where to insert? – Eminem347 Dec 02 '16 at 21:03
  • Don't belive this is the full code... How about this... instead of updating the `selected` variable whenever it changes try to access the the value when you need it – Endless Dec 02 '16 at 21:07
  • Have a few suggestion... care to join me on [jsfiddle](https://jsfiddle.net/#&togetherjs=qw5LzfNU6r)? – Endless Dec 02 '16 at 21:12
  • @Endless I tried ! and It worked!!! ;Thanks a lot...But still I am getting ...cant access remove property of null error which i will try and figure out !!! – Eminem347 Dec 02 '16 at 21:15

0 Answers0