1

I have a form on my website which is submitted by clicking a link. I am trying to create a custom context menu so that a user can open the submitted form in a new tab, if desired. Because the pages are generated dynamically with cgi, the parameters of the functions are different each time. I would like to trigger the function with those parameters after adding the attribute target="_blank" to the form element.

I am able to access the parameters by using document.getElementById("...").onclick and then using regexes to extract them. But this seems overcomplicated. I feel there must be a way to say, in effect, "do what you would have done if someone had left-clicked this a element". Here is some simplified code:

<a id = "link" style = "cursor:pointer" onclick = "postform('1')">link</a>
<FORM id = "remote"><input id = "input" type = "hidden" name = "firstkey" value = ""></FORM>

I have the JS in a separate file, it basically says:

function postform(valueone) {
document.getElementById("input").value = valueone;
document.getElementById("remote").submit();
}

what I have now for opening in a new tab is:

function contextmenu() {
  var parameters =document.getElementById("newtab").onclick;
  //regexes go here
  postform(parameters);
 }

What I am asking is, is there a way in a line or two to replace what I am doing inside the contextmenu function which would fire the onclick event attached to the link with its parameters?

4
  • 1
    lookup this and then pass it in for a reference to what was clicked Commented Apr 28, 2015 at 22:04
  • I'm not sure how that would look - my question as far as I can tell is not how to reference the element but how to trigger the event attached to it - if I wrote this.onclick for example, I would be in the same position I described in the question. Commented Apr 28, 2015 at 22:16
  • so you want to trigger a click on a given element? Commented Apr 28, 2015 at 22:24
  • essentially, yes - whatever would get the "onclick" attached to that element to fire Commented Apr 28, 2015 at 22:26

1 Answer 1

2

you can programmatically fire a click event of a node using its click method.

So for an element of id 'newtab':

document.getElementById("newtab").click();

*Note: wrap that in some error checking in case the node isnt found for some reason.

Sign up to request clarification or add additional context in comments.

Comments

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.