-2

So what I am trying to do here is when the submit button is clicked pull all of the values from the textboxes and display them in a p tag that I have on my html page. I have been tring to figure this out for days. Can somebody let me know what I am doing wrong?

/* JavaScript */

 $("#submit").click(function(){
var doc = $("#doctorate").val() 
var Name = $("#first_name").val();
var Last = $("#last_name").val();
var T = Doc + " "+ Name + " " + Last  
break
var Certs = $("#certs").val()
break
var Title = $("#title").val()
break
var Department = $("#department").val()
break
var Numb = $("#number").val()
break
var Web = $("#website").val()
break
var Ema = $("#email").val()
document.getElementById('output').innerHTML = T;
})

HTML unchanged

Jangbang
  • 9
  • 3
  • 1
    Have you used some debug prints (`console.log(...)`) to check whether your code (and what parts of it) is getting called? Try to produce a more to-the-point example that includes, say, only one field but also the relevant HTML. You may also want to consider using jQuery. – Petr Baudis Oct 06 '16 at 23:59
  • 2
    _“I dont know what I'm doing wrong”_ — then your first step should be to press `F12` and look at the error messages in the console; not ask on Stack Overflow. You can also look at the warnings using JSHint. `role` and `output` aren’t used, `checked` is not defined, `if (doctorate = checked)` is definitely not correct. The `value` property will never normally return `null`. `if (first_name || last_name != null)` does not check both `first_name` and `last_name` against `null`. – Sebastian Simon Oct 06 '16 at 23:59
  • 1
    @Xufox - Not to get into a whole thing here, but don't you think there's a nicer way to say that? Not everyone knows or has access to Developer Tools. – Christine Oct 07 '16 at 00:02
  • 1
    @Xufox I am just starting to code so I am very inexperienced and I pressed f12 and didn't see any error messages. This is all new to me so you can really tone down the aggression. I came here looking for help not to be berated. Seems I have struck some nerve with you, you could have simply not responded to this if it bothered you so much. – Jangbang Oct 07 '16 at 00:17

2 Answers2

0

The big problem I see is that you keep assigning the same thing to your output. Try something like this (reduced for brevity):

function myOut() {
    var output = document.getElementById("certs").value;
    output = output + document.getElementById("title").value;

    if (certs != null) {
        document.getElementById("output").innerHTML = output;
    }
}

<input type="text" id="certs" />
<input type="text" id="title" />
<p id="output"></p>
<button onclick="myOut()">
    Sub
</button>
Christine
  • 621
  • 7
  • 21
  • I tried that but the form just clears when I hit submit. here is a piece of my HTML Website:

    – Jangbang Oct 07 '16 at 00:18
  • @Jangbang - please edit the question to provide the HTML you are using, including the form tag. See also [how to submit without reloading the page](http://stackoverflow.com/questions/2866063/submit-form-without-page-reloading) and research sending form data to the server without submitting the form. – traktor Oct 07 '16 at 00:40
  • You're not calling the function from your submit button. Try adding onclick="myOut()" where "myOut" is the name of your funcrion. – Christine Oct 07 '16 at 01:31
0

Here you can try this one.

  var role = document.getElementById("role").value;
 var first_name = document.getElementById("first_name").value;
 var last_name = document.getElementById("last_name").value;
 var doctorate = document.getElementById("doctorate").value;
 var certs = document.getElementById("certs").value;
 var title = document.getElementById("title").value; 
    var department = document.getElementById("department").value;
 var number = document.getElementById("number").value;
    var website = document.getElementById("website").value;
 var email = document.getElementById("email").value;
 var output = document.getElementById("output").value;




$("#submit").click(function(){
var N = $("#first_name").val();
var L = $("#last_name").val();
var T = N + " " + L;


 
 if (doctorate.checked) {
 document.getElementById('output').innerHTML = T;
 }
 
    if (first_name != null) {
       document.getElementById('output').innerHTML = T;
    }
 
 if (last_name != null) {
       document.getElementById('output').innerHTML = T;
    }
 
 if (certs != null) {
       document.getElementById('output').innerHTML = T;
    }
 
 if (title != null) {
       document.getElementById('output').innerHTML = T;
    }
 
 if (department != null) {
       document.getElementById('output').innerHTML = T;
    }
 
 if (number != null) {
        document.getElementById('output').innerHTML = T;
    }
 
 if (website != null) {
        document.getElementById('output').innerHTML = T;
    }
 
 if (email != null) {
        document.getElementById('output').innerHTML = T;
    }
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
            <fieldset id="role">
                <legend>My Role</legend>
                <label for="faculty">
            <input type="radio" name="role" id="faculty" value="faculty" />
            Faculty
          </label>
                <label for="staff">
            <input type="radio" name="role" id="staff" value="staff" />
            Staff
          </label>
            </fieldset>
            <fieldset id="userinformation">
                <legend>User Information</legend>
                <label for="doctorate">
            Doctorate?
         <input type ="checkbox" id="doctorate" value="" />   
          </label>
                <label>
            First Name:
            <input type="text" name="name" id="first_name" required />
          </label>
                <label>
            Last Name:
            <input type="text" name="name" id="last_name" required />
          </label>
                <label>
            Certifications:
            <input type="text" name="cert" id="certs" />
          </label>
                <label>
            Title:
            <input type="text" name="title" id="title" required />
          </label>
                <label>
            Department:
            <select id="department" required>
                <option disabled selected value>-Select a Department-</option>
                <option>School of Information Technology</option>
                <option>Mathematics</option>
                <option>English</option>
                <option>History</option>
                <option>Natural Sciences</option>
                <option>Psychology</option>
                <option>School of Health Sciences</option>
            </select>
          </label>
          <label>
            Primary Phone #:  
            <input type="tel" name="number" id="number" placeholder="444-123-1234" pattern="^\d{3}-\d{3}-\d{4}$"/>

          </label>
                <label>
            Email:
            <input type="email" name="email" id="email" placeholder="JDoe@example.com" required />
            <span id="err3"></span>
          </label>
                <label>
            Website:
            <input type="text" name="website"  id="website" placeholder="http//:www.example" pattern="https?://.+" />
          </label>
            </fieldset>
            <fieldset id = "results">
            <p id="output"></p>
            </fieldset>
            <fieldset id="submit_button">
                <input type="button" id="submit" value="submit" />
            </fieldset>
  • Unfortunately its still just clearing the form when submit is clicked – Jangbang Oct 07 '16 at 01:05
  • @Jangbang try it. – Trafalgar D Law Oct 07 '16 at 01:11
  • It works but where would I put it. Maybe I put it in the wrong place and that's why it wasn't working. I mean I know where to put the source tag but what about the $submit – Jangbang Oct 07 '16 at 01:25
  • @Jangbang just follow my JS code above as sample and put it in your Javascript.. Then in your `` change it into `button` as sample in my code – Trafalgar D Law Oct 07 '16 at 01:27
  • When i change it to button nothing happens, and I inserted the JS into my own just to see if it would do the first and last name but nothing happens. Its like the button isn't working anymore. Its also not validating that the required boxes are filled – Jangbang Oct 07 '16 at 01:37
  • @Jangbang like you said in your post _when the submit button is clicked pull all of the values from the textboxes and display them in a p tag that I have on my html page_. What else the button function will do? – Trafalgar D Law Oct 07 '16 at 01:42
  • The code is working in the snippet but not when I add it to my JS, let me edit my earlier post and show you exactly what it looks like now. – Jangbang Oct 07 '16 at 01:49
  • @Jangbang in your if statements what is the purpose of that?. What do you to achieve? – Trafalgar D Law Oct 07 '16 at 02:11
  • nothing and I realize that now so I took them out along with everything else but the code you wrote plus what I have added to it to get all of the boxes. When I click submit literally nothing happens. Why does it work for you but not mine. I'll update again and show you what I have – Jangbang Oct 07 '16 at 02:19
  • Did you add your script to you html view?. – Trafalgar D Law Oct 07 '16 at 02:22
  • Yes, on my HTML page I called my CSS, the jQuery, then the form.js – Jangbang Oct 07 '16 at 02:27
  • I tried to run it in two different browsers and its not working. Nothing happens when I click submit. The values of the text boxes are not displayed in the p tag – Jangbang Oct 07 '16 at 02:37
  • No error in console other than a syntax error that I resolved. I'm not sure what the issue is – Jangbang Oct 07 '16 at 02:50
  • @Jangbang but did you notice that the name and last name is in your `p` tag? – Trafalgar D Law Oct 07 '16 at 02:55
  • No i tried running exactly as you had it but when I click submit nothing happens at all. The p tag doesn't get populated with anything like it did in the snippet – Jangbang Oct 07 '16 at 02:58
  • [Here](https://jsfiddle.net/q97o8bh7/) try. Add new html view and add this code inside it and add the scripts and your css. if nothing happen something is wrong. – Trafalgar D Law Oct 07 '16 at 03:02