0

I've been trying to call functions from a drop-down menu, but for some reason only shift(CPL) seems be called. I have verified that the functions themselves are working, and have redacted those for easier reading. I'm very new to Javascript and may have made a fairly basic error, apologies for that and thanks in advance!

HTML:

<select onchange="shift(this.value);">
  <option value="CPM">CPM</option>
  <option value="CPC">CPC</option>
  <option value="CPL">CPL/A</option>
</select>

Javascript:

function shift(CPM) {

}

function shift(CPC) {

}

function shift(CPL) {

}


</script>
2
  • 2
    Three function declarations with same name ? , or call single shift function with value of select as parameter ? Commented Sep 28, 2015 at 13:30
  • Ah, apologies. I meant to have a single function, and call it with various parameters as presented in the drop-down menu, but from comments it seems I have done this completely wrong... Would someone be so kind as to provide an example of how I'd structure a function that accepts the various parameters of the drop-down menu? Commented Sep 28, 2015 at 13:36

4 Answers 4

1

I think you didn't really understand how functions work in programming languages, i recommend you to study a little more and follow guides before trying to create something new.

Basically, you don't have to create 3 different shift function: one is more then enough: only the parameter will change. try this code:

function shift (genericParameter) {
  if (genericParameter == 'CPM') {
     //CPM
  } else if (genericParameter == 'CPC') {
     //CPC
  } else if (genericParameter == 'CPL') {
    //CPL
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Ah, thank you! My workplace is very much supportive of 'sledgehammer learning' when it comes to programming, but I will study the foundation concepts more vigorously and make sure to consolidate the fundamentals better. Thanks again.
we are here to help you :) of course practice is great for learning, just follow some live example at first :D
1

For how to do it in jquery:

DEMO

js code:

$(document).ready(function(){

  $('select').on('change', function() {
   shift(this.value);
  });

});

function shift(yourValue) {
    alert(yourValue) //Implement what you need here. 
}

//or maybe you prefer this:

function shift(yourValue) {
    switch(yourValue) {
        case 'CPM':
            //code block
            break;
        case 'CPC':
            //code block
            break;
        case 'CPL':
            //code block
            break;
        default:
            default code block
    }
}

hope it's helps.

3 Comments

Thanks very much! I'm not massively familiar with Jquery, but I'll take time to try this out. Thanks again for your contribution :)
you also can use the function shift with the switch with your code you don't need jquery for that if you want to avoid use if else @RFM_Euan
Thanks, I'll keep that in mind and have a play around with the code you provided.
1

Try creating an object having properties of select values , at selection do stuff corresponding to property name of object

var selected = {
  "CPM":"option 1",
  "CPC": function(v) {
           return v.toLowerCase()
  },
  "CPL": "option 3"
}

function shift(val) {
  var res = typeof selected[val] === "function" 
            ? selected[val](val) 
            : selected[val];
  alert(res)
}
CPM
<select onchange="shift(this.value);">
  <option value="CPM">CPM</option>
  <option value="CPC">CPC</option>
  <option value="CPL">CPL/A</option>
</select>

1 Comment

Thanks for the contribution, it looks fairly abstract to my untrained eye... but I'll dive in and have a go until I understand. Thanks again :)
1

i think your missing just syntax , this can help you..

enter code here

<select id="selectDrop" onchange="copy();">
    <option value="">Default:</option>
    <option value="one" >one</option>
    <option value="two" >two</option>
    <option value="three" >three</option>
  </select>

JavaScript :-

  function copy(){
     alert(document.getElementById("selectDrop").value);
     }

1 Comment

Thank you, this looks like in future it would be a good test to check that a drop-down menu is working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.