Get a value of an input with Jquery [duplicate]

0

I have to make a page that thinks of a random number and then with an input enter that number, if it is not the random number that tells me if it is smaller or bigger.

Well, what I find a problem is that when I input an input does not tell me if it is greater or less, eye! It has to be without pressing a submit or anything that tells me if it is bigger or not

var docCookies = {
  getItem: function(sKey) {
    return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\s*" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\$&") + "\s*\=\s*([^;]*).*$)|^.*$"), "$1")) || null;
  },
  setItem: function(sKey, sValue, vEnd, sPath, sDomain, bSecure) {
    if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) {
      return false;
    }
    var sExpires = "";
    if (vEnd) {
      switch (vEnd.constructor) {
        case Number:
          sExpires = vEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + vEnd;
          break;
        case String:
          sExpires = "; expires=" + vEnd;
          break;
        case Date:
          sExpires = "; expires=" + vEnd.toUTCString();
          break;
      }
    }
    document.cookie = encodeURIComponent(sKey) + "=" + encodeURIComponent(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : "");
    return true;
  },


  removeItem: function(sKey, sPath, sDomain) {
    if (!sKey || !this.hasItem(sKey)) {
      return false;
    }
    document.cookie = encodeURIComponent(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "");
    return true;
  },
  hasItem: function(sKey) {
    return (new RegExp("(?:^|;\s*)" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\$&") + "\s*\=")).test(document.cookie);
  },
  keys: /* optional method: you can safely remove it! */ function() {
    var aKeys = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:|$)/g, "").split(/\s*(?:\=[^;]*)?;\s*/);
    for (var nIdx = 0; nIdx < aKeys.length; nIdx++) {
      aKeys[nIdx] = decodeURIComponent(aKeys[nIdx]);
    }
    return aKeys;
  }
};

function numeroAleatorio(min, max) {
  return Math.round(Math.random() * (max - min) + min);
}
var random = numeroAleatorio(1, 100);
var valor;
if (docCookies.hasItem("comptador")) { //Existeix la cookie comptador?
  random = parseInt(docCookies.getItem("comptador")); //obté la cookie, però és una cadena, així que la pasa a enter
  docCookies.setItem("comptador", random, new Date(2020, 5, 12)); //Desem la cookie per a que quan es recarregue la pàgina, el navegador s'enrecorde del seu valor

} else {
  docCookies.setItem("comptador", random); //Desem la cookie per a que quan es recarregue la pàgina, el navegador s'enrecorde del seu valor
}
<input type="text" name="num" value="" size="20" /> Recarrega la pàgina per veure el valor del comptador. <br /><br />
<a href="" onclick="docCookies.removeItem('comptador')">Esborra la cookie</a>
    
asked by Oriol 26.10.2017 в 20:10
source

1 answer

0

In the html you would have to put to put the field in which to enter the number:

<input type="number" id="loquequieras">

And then in the jquery in the head (between labels) you should put like when you use a selector in CSS, it would be something like this:

var x = $("#loquequieras").val();

The $ symbol is for you to do with jquery and the # for you to take it as ID.

    
answered by 26.10.2017 в 21:51