0

I'm in need from javascript thrigger a data-action for my button. I can't see a good way to do it.

I have this button:

echo '<button type="button" class="btn btn-sm btn-secondary" id="view" data-id="'.$row_data['id'].'" data-function="view" data-action="side_overlay_toggle" data-idioma="#" title="View Info">';
echo '<i class="fa fa-eye"></i>';
echo '</button>';

I have a function to do event handler since I need to do some queries.

$('#reloadedituser').on( "click", "button", function(e) {
e.preventDefault();
let button = $(this); // the button you clicked
let id = button.data("id"); // the data-id attribute of the button
let func = button.data("function");

if(func == "view"){
    e.document.getElementById("view").click();
}
});

So, the javascript reads the code, and if the data-function is "view", I would it to thrigger the data-action from that button (side_overlay_toggle). How can I achieve that? It seems that .click doesn't make it thrigger the button. Any suggestion in this?

Also, can someone help me, before the data-action, make a query and add it to a specific div? My problem is not doing the query itself but how to handle it in javascript. Thanks.

Tiago
  • 625
  • 5
  • 16
  • change e.document.getElementById("view") ... to $('#view').trigger('click'); – daremachine Aug 31 '18 at 23:05
  • no need to trigger the click for the same button .. you'll make an unexpected loop of clicks.. you can directly put the action `if(func="view"){console.log(button.data("action"));}` .. **Id should be unique so don't duplicate id** and if you just need the click for the button with `data-function = "view"` you can simply use `.on( "click", "button[data-function='view']", ` without need to use if statement – Mohamed-Yousef Aug 31 '18 at 23:09
  • This button is dinamically created so I will have severall buttons. When I click a button, I will make a query to the BD and then add the information to a DIV that should trigger the data-action (will show a sidebar). So what's the recommended approach? Assume that's a user list, each user with the view button. It should get the user information and then slide the sidebar with the information from the user. Thanks. – Tiago Aug 31 '18 at 23:18
  • db? server side? so you'll need to use **$.ajax** and pass the `data-id` value to the server to get the information then return it back .. [see answers here](https://stackoverflow.com/questions/9436534/ajax-tutorial-for-post-and-get) – Mohamed-Yousef Aug 31 '18 at 23:23
  • Right. That task is done. My problem is to trigger the sidebar using javascript. Cause first I will make the query, then I will trigger the side bar. Just need from javascript to trigger data-action. Basically, I need the button to trigger JS data-function (that's working) and then javascript trigger the data-action. The first part is because I have several buttons inside that div (edit, delete, view, etc). It will read other buttons too and call different functions. – Tiago Aug 31 '18 at 23:27

0 Answers0