0

I am trying to pass the selected values from the tag and pass it to a button’s click event. However, I was not able to get the data in the onclick function. See the code below.

I am getting undefined in the alert. Please help.

<div class=“form-group”>
   <div class=“form-group” align=“center”>
      <label for=“select-Year”>Select CMS Fiscal Year :</label>
      <select class=“form-control input-sm” style=“width:120px” id=“select-Year”>     
         <option v-for=“yr in years” value=“yr”>{{ yr }}</option>
      </select>
   </div>

   <br/><br/>

   <div class=“form-group” style=“align:center”>
      <button id=“btnSubmit” class=“btn btn-primary” style=“align:center” v-on:click=“loadData(yr, $event)”>Open</button>
   </div>
</div>

methods: {
  `loadData`: function(yr, event){
    alert(yr);
  }
}

2 Answers 2

3

The yr variable only exists within the scope of option when you used v-for on it.

That's why it's causing an error when you try to pass it to the event handler of your button which is located outside the scope of v-for.

How can I get selected option?

One way to get the year selected is to declare a year variable under your component data attribute and use the v-model directive on your select field to form a two-way binding.

data: function() {
  return {
    year: null
  }
}

And in your select and button tags,

<select class="form-control input-sm" style="width:120px" id="select-Year" v-model="year">
  <option v-for="yr in years" value="yr">{{ yr }}</option>
</select>
<button id="btnSubmit" class="btn btn-primary" style="align:center" v-on:click="loadData($event)">Open</button>

In this way, you can get access to the year in loadData,

loadData(event) {
  console.log(this.year, event);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Ana, thanks for your response. i am creating that whole html code as a template using the <script type="text/x-template" id=""></script> option. for this vue.component() i have already have a data from where i am getting the array values for the for loop. Do i have to give the v-model for a property in that same vue.component or the vue instance ??
Hey @Mani079, can you create a fiddle for this? It would make it faster and easier to resolve which shows how it's being used in another component.
2

I tried to do this for you, go to the page and see <https://jsfiddle.net/Lr3psu2y/>.
Hope it with help for you.

1 Comment

Thanks man. your code works perfectly. but it also throws a warning in the console as below "<option v-model="yrInd">: v-model is not supported on this element type. If you are working with contenteditable, it's recommended to wrap a library dedicated for that purpose inside a custom component.". will it make any problem ?

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.