-1

I would like to change the following onclick event, from:

view_tree_element('subscription','45592063',1);
focusit('1_45592063',true)

to

onclick="show_dialog('feed_info_dialog',{subscription_id: 45592063}

i.e. get the number 45592063 from the original onclick event,

store it in a variable and change the anchor to the show_dialog event

How would I do this?

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • 3
    Welcome to SO. Please visit the [help] to see what and [ask]. Hint: Post effort and code – mplungjan Dec 15 '17 at 09:03
  • Why would you want to do that? – Adriano Dec 15 '17 at 09:06
  • because the first function redirects in the same window (and you have to go back to the initial page afterwards), while the second opens a popup, which is more convenient, you don't have to leave the page – patrickdrd Dec 15 '17 at 09:15

1 Answers1

0

Have a look at this. It replaces the onclick handlers if there are any. You can add a test for existence of "subscription" in the onc var

I store the ID in an attribute of the link itself

var re = /'subscription','(\d+)'/; // a number in quotes after 'subscription',
window.onload=function() { // or addEventListener
  var links = document.querySelectorAll("a[onclick]");
  for (var i=0; i<links.length;i++) {
    var onc = links[i].onclick.toString();
    if (onc) {
      var id = onc.match(re);
      if (id) { // something found
        links[i].setAttribute("sub-id",id[1]);
        links[i].onclick = function() {
          show_dialog('feed_info_dialog',{subscription_id: this.getAttribute("sub-id")});
          return false; // cancel link
        }  
      }
    }  
  }
}
// this will of course be YOUR show_dialog
function show_dialog(a,b) { 
  console.log(a,b);
}
<a href="#" onclick="view_tree_element('subscription','45592063',1);
focusit('1_45592063',true)">Click</a><br/>
<a href="#">No onClick</a><br/>
<a href="#" onclick="alert('subscription','',1)">Click - no id</a><br/>
<a href="#" onclick="view_tree_element('subscription','55592063',1);
focusit('1_55592063',true)">Click</a>
mplungjan
  • 169,008
  • 28
  • 173
  • 236