0

I dynamically create a button element in my javascript function and then add an onclick event to that button. But after I press that button I get this error Uncaught ReferenceError: removeQuestionis not defined (removeQuestionis is my function name )

this is the button i created inside of my js function

const addQuestions = () => {
   const removeBtn = '<a color="primary" onClick={removeQuestion}> x</a>';
   window.document.body.insertAdjacentHTML('beforeend', removeBtn);
};

this function are called by onclick

const removeQuestion = () => {
        console.log('test')
    };
6
  • How are you loading the javascript? Commented Jul 14, 2022 at 16:47
  • can you try adding onClick={this.removeQuestion} Commented Jul 14, 2022 at 16:50
  • 1
    Please, try to add the full example of code. Commented Jul 14, 2022 at 16:51
  • Thanks @Mina update my full code related to this issue Commented Jul 14, 2022 at 17:00
  • 2
    You're using insertAdjacentHTML in a React app when you shouldn't be (React deals with DOM updates and doesn't understand when another process interferes). You should add your button to your JSX and show it conditionally based on some state that is triggered at some point. Commented Jul 14, 2022 at 17:14

3 Answers 3

1

ReactJS

Read carefully the Refs and the DOM part of the React documentation.

React is working using virtual DOM.

export default function App() {
  const removeBtns = ['x1', 'x2'];
  const removeQuestion = () => {
    console.log('test');
  };
  return (
    <div className='container'>
      {removeBtns.map((x) => (
        <a key={x} href={'#!'} onClick={removeQuestion}>
          {x}
        </a>
      ))}
    </div>
  );
}

HTML

Try onClick="removeQuestion()" or onClick={removeQuestion()}

The value accepted by onclick in html is the javascript statement executed when clicked.

The value accepted by onclick in javascript is the callback function executed when clicked.

const removeBtn1 = '<a onClick={removeQuestion()}> x1 </a>';
const removeBtn2 = '<a onClick="removeQuestion()"> x2 </a>';

const removeQuestion = () => {
  console.log('test')
};

window.document.body.insertAdjacentHTML('afterbegin', [removeBtn1, removeBtn2]);

Also you can try EventListener.

const removeBtn1 = '<a class="btn"> x1 </a>';
const removeBtn2 = '<a class="btn"> x2 </a>';

window.document.body.insertAdjacentHTML('afterbegin', [removeBtn1, removeBtn2]);

document.querySelectorAll('.btn').forEach(btn => {
  btn.addEventListener('click', () => {
    console.log('test');
  });
});

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

2 Comments

i tryed both of this onClick="removeQuestion()" and onClick={removeQuestion()} but still getting the same error :(
@Vivekkalanka Sorry, I didn't notice it was reactjs at first, I re-edited and added an example for your reference
1

this is issue with insertAdjacentHTML you can use ReactDOM.render

 let input = React.createElement("input",{className:"questionTextInput",name:"textInputQuestion[]"},null);
   ReactDOM.render(input,questionPreview);

Comments

0

this is issue with insertAdjacentHTML i used React.createElement and after rendered using ReactDOM.render

let input = React.createElement("input",{className:"questionTextInput",name:"textInputQuestion[]"},null);
   ReactDOM.render(input,questionPreview);

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.