0

sas per my title i'm trying to concatenate 3 textboxes that have dynamic values based on radio button presses. I'm very new to HTML and Java so have literally been self teaching off google to come up with this so it's probably messy. The code so far is as follows:

http://jsfiddle.net/jkxbwLwt/

<input type="radio" name="r3" onclick="myFunction2(this.value)" value="CA$!">Only want to talk to case owner<br>
<input type="radio" name="r3" onclick="myFunction2(this.value)" value="VO$!">Transfer to Voicemail<br> 
<input type="radio" name="r3" onclick="myFunction2(this.value)" value="EM$!">No EOS entry required<br>

<input type="radio" name="r3" onclick="myFunction2(this.value)" value="NA$!">N/A<br><br>
<input type="text" id="result2">

  </form>

Here is just a small exerpt of how 1 part of the code works. The rest is in the jsfiddle.

I've tried referencing the ID with Javascript but it won't update so I think i'm getting the syntax wrong. This is all being done in notepad (not access to notepad++ or anything better)

Thank you very much, please let me know if I can provide more information.

Tom Hunt
  • 3
  • 3
  • Thanks for that! Have made the edit. As I said i'm new to this. – Tom Hunt Jul 01 '15 at 23:36
  • That would be easy to do with jQuery. – aleksandar Jul 02 '15 at 00:44
  • True, I don't really know too much about it. I've come across some examples while searching for this answer but don't know how to encorporate it correctly. My main problem is trying to turn my "result2" ID into something I can concatenate. I have 3 textboxes known as result1-3 that are dynamic. – Tom Hunt Jul 02 '15 at 01:31

2 Answers2

1

automatically copy to clipboard could be dangerous. But you can following the instruction from this stackoverflow post to concatenate all of the text together and let users do it manually.

How do I copy to the clipboard in JavaScript?

I have a little jsfiddle link here

function copyToClipboard() {
    var result1=document.getElementById("result1").value,
    result2=document.getElementById("result2").value,
    result3=document.getElementById("result3").value,
    text=result1 + result2+ result3; 

  window.prompt("Copy to clipboard: Ctrl+C, Enter", text);
}

https://jsfiddle.net/dshun/jkxbwLwt/11/

Community
  • 1
  • 1
dshun
  • 223
  • 2
  • 9
  • Hey, the way that prompt appears is PERFECT I didn't even know that was possible! Though (maybe due to my browser) when I select all 3 options it only comes up with the 3rd selection box in the copy window. Just because of the dodgy way it was coded the order they'd ideally appear is "result3result1result2" all in one word, in this it appears "result2" Thank you so much for your time. – Tom Hunt Jul 02 '15 at 01:36
  • hi, sorry i sent you the wrong jsfiddle link, because i was having a little trouble copying/pasting code in stackoverflow. I aslo changed the order of the result, so final result is result3result1result2 – dshun Jul 02 '15 at 03:20
0

Hey Tom I coded this a earlier today and then had to hop off of my computer but it seems to me that you are doing a lot of extra code for yourself as well as calling onclick function in your view a million times. To make this work.

With forms in general it is not a good habit to have multiple forms on one page. As well with this type of option it might be easier to use select form elements instead of multiple choice.

In my example I mocked up a situation that uses both select elements and multiple choice input elements

As I mention before adding onclick functions to your HTML isn't a very good habit and should be avoided if possibly to keep your code as DRY(Don't Repeat Yourself) as possible. You may want to take a look at javascript EventListeners

I have commented my jsfiddle code to hopefully clear up any issues here is a link to the code and the what you can do to help clean up you html. Whats cool about this is that is will allow you to add more multiple choice types or select inputs and will continue to work no matter and you only have to adjust your html to make it work https://jsfiddle.net/kriscoulson/cvwspxo9/2/

var selects = document.getElementsByTagName('select'),
radios = document.querySelectorAll("input[type='radio']"),
selectButton = document.getElementById('select-button'),
choiceButton = document.getElementById('choice-button'),
answerBox = document.querySelector('.answer');

//This is the code to add and event listener to the get select values button
selectButton.addEventListener('click',function(event){

    event.preventDefault(); // We prevent the default action when a button is clicked inside a form
    var selectArr = [] // We create an empty array to push all of our values into


    for(var i = 0; i < selects.length; i++){
        // We then loop through of the selects and push the value of each select to our array
        selectArr.push(selects[i].value)
    }

    //You can change this join method to concatnate the strings instead of having them seperated by commas
    var string = arr.join(", ");

    //The code below adds a new input with our values as  string inside.
    answerBox.innerHTML = "<input class='copy' value='"+string+"'/>";
    answerBox.children[0].select(); //We can then select the the code to make it easy to copy to the clipboard
});


//This is the code to add and event listener to the get multichoice values button
choiceButton.addEventListener('click', function(ev){
    ev.preventDefault(); // We prevent the default action when a button is clicked inside a form
    choiceArr = [] // We create an empty array to push all of our values into

    for(var i=0; i < radios.length; i++){ // We then loop through of the radios

        if(radios[i].checked){ // If the radio at the index is checked 
            // we push that indexs value to our choice array
            choiceArr.push(radios[i].value);
        }    

    }
    //You can change this join method to concatnate the strings instead of having them seperated by commas
    var string = choiceArr.join(", ");

    //The code below adds a new input with our values as  string inside.
    answerBox.innerHTML = "<input class='copy' value='"+string+"'/>";
    answerBox.children[0].select();//We can then select the the code to make it easy to copy to the clipboard

});
Enjayy
  • 1,074
  • 8
  • 18
  • Wow thank you very much! I don't know too much about the good habits yet (though am slowly learning via guides online) so this is extremely valuable. Thanks a million! – Tom Hunt Jul 02 '15 at 21:00
  • @TomHunt Glad I could help!! You will learn best practice with time keep up the good work and keep coding! – Enjayy Jul 02 '15 at 21:05