0

I'm trying to change my 'send' button which onClick sends a message from an input box to a div, to also trigger the click of a 'checkbox' input.

HTML

<button type="submit" class="btn btn-default">Post</button> 
   <input name="stry" type="text" id="stry"/> 
   <input name="nope" type="text" id="message-input"/>
        <input type="checkbox" name="sendsms" onclick="copyStory(this)">

*These buttons are actually in a form

JS

 function copyStory(ch) {
     if (ch.checked)
         var text1 = document.getElementById("message-input").value;
     else
         text1 = '';
     document.getElementById("stry").value = text1;
 }

I've searched around but I can't find a way to make the send button trigger the checkbox, any suggestions?

2
  • 2
    Are you using jQuery? And where is send button? Commented Feb 3, 2015 at 9:14
  • jQuery('[name="sendsms"]').click()? Commented Feb 3, 2015 at 9:27

1 Answer 1

1

So what you need is to have same handler for both onclick and onchange events. Try this way,

HTML :

<input type="button" id="sendButton" value="Send" onclick="copyStory(this)" />
<input name="nope" type="text" id="message-input"/>
<input type="checkbox" name="sendsms" onchange="copyStory(this)"/>

<div id="msgDiv"></div>

javaScript :

 function copyStory(ch) {
     var text1
     if (ch.checked || ch.id == "sendButton")
         text1 = document.getElementById("message-input").value;
     else
         text1 = document.getElementById("msgDiv").innerText = "";

     document.getElementById("msgDiv").innerText = text1;
 }

jsFiddle

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

2 Comments

I don't see why you changed document.getEelemntById..... code? It seemed fine to just change the 'if' part
op mentioned to send the message from an input box to a div. But didn't find any div to show the message. So created a div and also removed the unnecessary codes irrelevant to this problem.

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.