4

I am using this library - datetimepicker_css.js in my html/jquery code. Whenever I click on the image, a time format like mm:dd:yyyy hh:mm:ss populates in input tag.

 Start time:<input type="text" id="demo1" maxlength="25" size="25" name="stime" />                                                                                                                        
 <img src="images2/cal.gif" onclick="javascript:NewCssCal ('demo1','yyyyMMdd','dropdown',true,'24',true)" style="cursor:pointer"/>   

I am trying to implement same in react js code, and facing issues doing so. I could populate start_time but couldn't do a onclick of image tag and replace in the start_time field. Any help/suggestion on this is highly appreciated.

  render() {
        return (
                <form onSubmit={this.handleSubmit}>
                    <input value={this.state.first_name} onChange={this.setFirstName} placeholder="First name"/>
                    <input value={this.state.last_name} onChange={this.setLastName} placeholder="Last name"/>
                    <input value={this.state.start_time} onChange={this.setStartTime} placeholder="Start Time" id="demo1" name="stime"/>
                    <img src="images2/cal.gif" onclick="javascript:NewCssCal ('demo1','yyyyMMdd','dropdown',true,'24',true)"/>
                    <button type="submit">Submit</button>
                </form>

        )
    }

    getInitialState() {
        return {
            first_name: "John",
            last_name: "Doe",
            start_time: "03/11/2016",
        };
    },         
   setStartTime(event) {
        this.setState({start_time: event.target.value});
    },                                                                     
6
  • what is your html for image? Commented Mar 11, 2016 at 11:59
  • @KishoreBarik edited the code Commented Mar 11, 2016 at 12:01
  • 1
    change onclick to onClick in image tag Commented Mar 11, 2016 at 12:03
  • @KishoreBarik Got this error : I am not sure how to use Image event as function. Uncaught Error: Invariant Violation: Expected onClick listener to be a function, instead got type string Commented Mar 11, 2016 at 12:05
  • ok now your onClick event is getting triggered. Now the error you are getting because the function you provided as string where it should be a function. you can bind a function of the component which will call NewCssCal ('demo1','yyyyMMdd','dropdown',true,'24',true) Commented Mar 11, 2016 at 12:10

1 Answer 1

4
render() {
        return (
                <form onSubmit={this.handleSubmit}>
                    <input value={this.state.first_name} onChange={this.setFirstName} placeholder="First name"/>
                    <input value={this.state.last_name} onChange={this.setLastName} placeholder="Last name"/>
                    <input value={this.state.start_time} onChange={this.setStartTime} placeholder="Start Time" id="demo1" name="stime"/>
                    <img src="images2/cal.gif" onClick={this.populateDate}/>
                    <button type="submit">Submit</button>
                </form>

        )
    },
populateDate(){
   NewCssCal('demo1','yyyyMMdd','dropdown',true,'24',true);

},

    getInitialState() {
        return {
            first_name: "John",
            last_name: "Doe",
            start_time: "03/11/2016",
        };
    },         
   setStartTime(event) {
        this.setState({start_time: event.target.value});
    },

Note: make sure your NewCssCal function is globally available

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

6 Comments

This works. But when i try to setState the input tag, I face an error . <input value={this.state.start_time} onChange={this.setStartTime} placeholder="Start Time" id="demo1" name="stime"/> <img src="images2/cal.gif" onClick={this.populateDate}/> setStartTime(event) { this.setState({start_time: event.target.value}); }, populateDate(event){ NewCssCal('demo1','yyyyMMdd','dropdown',true,'24',true); this.setState({start_time: event.target.value}); },
Error in the sense, I am changing the state of input tag value after the image click event. This needs to be updated as the target.value of input field but its not! Its still showing the previous value ( default ) of the input field. Does this not work? populateDate(event){ NewCssCal('demo1','yyyyMMdd','dropdown',true,'24',true); setStartTime(event); },
have a look on jsfiddle.net/KishoreBarik/xrnbb63m/1 it's working there. if it's still not working for you remove value={this.state.start_time} from demo1 input and check
This is my code : jsfiddle.net/adwantgoutam/cfthcqLk/2 I tried it all, but no luck.
your fiddle is not even running. Have you forked reacjs properly? I can't test there
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.