0

Attempting to pass ids dynamically breaks function:

<p id="email1" onclick="mailTo(this.id,'com','abc','info','My Website','I have a question for you: ')">Send us an email</p>
<p id="email2" onclick="mailTo(this.id,'org','xyz','support','My Other Website','I want to report a problem with your website.')">Report Website Problems</p>

Hard coding document.querySelector('#email') is successful, but need id to be dynamic. Console prints var correctly. Err: qS.addEventListener is not a function.

function mailTo(idx, tld, domain, account, site, bodyText) {
  let qS = `document.querySelector('#${idx}')`;
  console.log(qS);
  let arrEmail = [tld, domain, account, site, bodyText];
  const buildEmail = arr => `${arr[2]}@${arr[1]}.${arr[0]}?subject=From%20the%20${arr[3]}%20website&body=${arr[4]}`;
  qS.addEventListener("click", event => {
    let str = `mailto:${buildEmail(arrEmail)}`;
    location.href = str;
  });
}

Sandbox: https://codesandbox.io/s/p8k45v350

2
  • 1
    1. qS is a string, 2. Why would you add a new click event handler in a click event handler named mailTo? O.o Commented Feb 20, 2019 at 16:53
  • Right. eListener was for a single instance with the id hard-coded. Function re-written with eListener method removed. Commented Mar 1, 2019 at 22:53

3 Answers 3

2

Build just the selector param dynamically...

let qS = document.querySelector(`#${idx}`);
Sign up to request clarification or add additional context in comments.

Comments

2

This will help you:

function mailTo(...items)
{
console.log(items[0].id);
}
<p id="email1" onclick="mailTo(this,'com','abc','info','My Website','I have a question for you: ')">Send us an email</p>
<p id="email2" onclick="mailTo(this,'org','xyz','support','My Other Website','I want to report a problem with your website.')">Report Website Problems</p>

6 Comments

What is the problem with the code? What have you changed, and why would this help?
@Andreas, Sorry. in my solution, instead of passing the id of the element into the function and retrieve the element using it's id, I pass the element it self into the function and it will enables me to use the element or even it's id inside the function easily.
Can you explain the problem, maybe I didn't get the question point.
The problem is the line let qS = ... and not how to get the id (directly as parameter or from the passed element)
@Andreas, So I think that line of code is trying to retrieve the selected element. Am I wrong?
|
0

If you change

    let qS = `document.querySelector('#${idx}')`;

to

    let qS = this;

your code works. But you don't need to supply the id to query for the element since you can access it with this.

1 Comment

this loads the DOM so every click thereafter reinitiates the function.

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.