Paweł Naworol
Paweł Naworol

Reputation: 1

How to add option value to url

I am working on a calculator tax.

I wanted to receive the selected values of that was redirected to the url value, so that I could for example send someone a link to the values that I have chosen, so it does not have to set them again only to be had when you start link.

http://kalkulator.atrocki.pl/calculator/index.html

 <div class="styled-select">
        <h1>Vat</h1>
        <select id="vat">
      <option value='0'>0%</option>
      <option value='0.08'>8%</option>
      <option value='0.23'>23%</option>
      <option value="other">Add another VAT value..</option>
    </select>
        <input type="text" class="vatInput" id="vatInputId" placeholder="Vat w %">
    </div>
    <div class="styled-select">
        <h1>tax 2</h1>
        <select id="tax">
              <option value="0">0%</option>
              <option value='0.18'>18%</option>
              <option value='0.19'>19%</option>
              <option value='0.32'>32%</option>
              <option value="other">Add another TAX value..</option>
          </select>

Upvotes: 0

Views: 1086

Answers (2)

Lucas Borges
Lucas Borges

Reputation: 142

JavaScript has no built in function for handling query string parameters.

It's very easy to do this with whatever back-end language you are using in your server.

If you absolutely must do this with Javascript, check this question:

How to get the value from the GET parameters?

Upvotes: 1

GantTheWanderer
GantTheWanderer

Reputation: 1292

You can parse the query string into a nice object, then look for the key value pair you want.

function parseQuery() {
    var query = {};

    var indexOfQ = window.location.href.indexOf("?");

    if (indexOfQ < 0)
        return null;

    var a = window.location.href.substr(indexOfQ + 1).split('&');
    for (var i = 0; i < a.length; i++) {
        var b = a[i].split('=');
        query[decodeURIComponent(b[0])] = decodeURIComponent(b[1] || '');
    }
    return query;
}

function loadQueryString() {

    var query = parseQuery();

    if (!query) return;

    if (typeof query['val'] !== 'undefined') {
        var newSelection = $("<option>", { value: query['val'], text: (Math.floor(parseFloat(query['val']) * 100)) + '%'});
        $('select').prepend(newSelection);
    }
}

loadQueryString();

Upvotes: 0

Related Questions