N
N
Ninja Mate2016-02-06 00:18:45
JavaScript
Ninja Mate, 2016-02-06 00:18:45

How to connect an event to a button by class name?

A script with an onClick event was written in react, js was compiled.
The question is how to implement an event for buttons by class name, but without creating the buttons themselves through a script.
Now it looks like this, and inside the buttons with the class name booking, other buttons are created, to which the event is set.

var App = React.createClass({
    getInitialState() {
        return { smShow: false};
    },

    showModal() {
        this.setState({show: true});
    },

    hideModal() {
        this.setState({show: false});
    },

    render() {
        let smClose = () => this.setState({ smShow: false });

        return (
            <ButtonToolbar>
                <Button bsStyle="primary" onClick={()=>this.setState({ smShow: true })}>
                    Contact Form
                </Button>

                <MySmallModal show={this.state.smShow} onHide={smClose}/>
            </ButtonToolbar>
        );
    }
});

ReactDOM.render(<App />, document.getElementById('booking'));

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Deodatuss, 2016-02-06
@victorzadorozhnyy

so does not roll?

componentDidMount(){
  var btns = document.querySelectorAll('.btn');
    for (var i = 0; i < btn.length; i++) {
        btn[i].addEventListener("click",()=>this.setState({ smShow: true }));
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question