question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to pass data to callback?

See original GitHub issue

Hi, probably very naive question. How do I pass data to the callback method?

I would like to make an AJAX call to an API on select and clear methods, but I am trying to figure out how to pass information necessary for the API call.

Since content can be generated automatically, I usually set ids for ajax calls in the elements. For example: <button id="myButton" data-id="10"> </button> and then I can access that id in jquery. But I can’t do this with bar-rating since it replaces the default select element.

Any hints would be appreciated!

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
duffettcommented, Dec 4, 2019

The following worked for me HTML:

<select class="priority-widget" data-field-name="a">
   <option>1</option>
   <option>2</option>
</select>
<select class="priority-widget" data-field-name="b">
   <option>1</option>
   <option>2</option>
</select>

Script:

$('.priority-widget').barrating({
  theme: 'bars-pill',
  onSelect: function (value, text) {
    var fieldValue= this.$elem.data().fieldName;
     // a or b
    console.log(fieldValue);
  }
}); 
0reactions
marwan2commented, Jul 24, 2016

Your solution is print “undefined”

$('#example-pill').barrating('show', {
  theme: 'bars-pill',
  onSelect: function (value, text) {
    var id = $('#example-pill option:selected').data('id');
    console.log(id);
  }
});

So it doesn’t work !!!

Read more comments on GitHub >

github_iconTop Results From Across the Web

JavaScript: Passing parameters to a callback function
Just use the bind() function which is primarily used to set the this value. However, ...
Read more >
JavaScript Passing parameters to a callback function
In this article, we will see how to pass parameters to a callback function. Firstly let us see what is a callback function....
Read more >
How to pass a Variable or Argument to a callback function in ...
The assignment is simple. Search user with the name given, update the address of the user and print the user details after the...
Read more >
Callback Function Javascript
A JavaScript Callback is a function passed as an argument to another function. In the above example, we passed in as an argument...
Read more >
JavaScript Callback Functions – What are Callbacks in JS and ...
In JavaScript, the way to create a callback function is to pass it as a parameter to another function, and then to call...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found