brio
brio

Reputation: 175

Post data to JS

I am not sure how to ask my question...

I have a problem with my form. When I select "Qebele", it calls getline() in Line.js. I need to post any value to line.js and in line.js get this data to use it. How can I do this?

<form action="">
      <li><select id="rayon" onchange="getline();">
          <option value="">Select Rayon</option>
          <option value="rayon">Qebele</option>
      </select></li>
</form> 

Upvotes: 0

Views: 106

Answers (4)

BenM
BenM

Reputation: 53246

You can easily get the value of #rayon inside Line.js using the following code:

function getline()
{
    var theLine = document.getElementById('rayon').value;
    alert(theLine);
}

Here's a jsFiddle

Upvotes: 2

Punit Sachan
Punit Sachan

Reputation: 593

You can pass this.value in onchange event.

<form action=""> <li><select id="rayon" onchange="getline(this.value);"> <option value="">Select Rayon</option> <option value="rayon">Qebele</option> </select></li> </form>

function getline(selectedValue){
  if(selectedValue == 'rayon'){
    //add your logic here
  }
}

Upvotes: 0

Andrew De Rozario
Andrew De Rozario

Reputation: 19

I am not sure what your asking. Looking at your code you should not use an inline function handler as the 'this' keyword will refer to the window object. If you use

document.getElementById('rayon').addeventlistener('change',getline);

in line.js the 'this' keyword will refer to the select element with the id of 'rayon'.

Therefore

function getLine(){

alert(this.value) }

will give you the value of rayon when you change the select to Qebel.

Obviously you will inplement your own logic to suit your needs. It is very important not to use inline function calls as the 'this' keyword has a different meaning.

Upvotes: 0

KernelPanik
KernelPanik

Reputation: 8339

You can try passing a reference of the selected option's value to function getline, then continue from there.

For example, to pass a reference of the selected option's value to getline...

<form action="">
            <li><select id="rayon" onchange="getline(this.options[selectedIndex].value)">
                <option value="">Select Rayon</option>
                <option value="rayon">Qebele</option>
            </select></li>
</form> 

Then, in Line.js, getline() does something based upon what was selected.

function getline(selectedValue){
    if (selectedValue == "something"){
             ... do something
    }

}

Upvotes: 1

Related Questions