0

I'm making a simple form and I'm having trouble getting it to do what I want. On form submission I need it to detect if multiple inputs of a certain category have values greater than 0 (because only one should be selected). Here's the html of the form

      <form onsubmit="calcTime();">

  <table>
    <tr>
      <p>How long does the task take to perform?  (Choose one)</p></tr>
    <tr>
      <p>
        <input type="text" style="width: 3em;" id="seconds">&nbsp;  Seconds (Enter an integer 1-60)
        <input type="text" style="width: 3em;" id="minutes">&nbsp;  Minutes (Enter an integer 1-60)
      </p>
    </tr>
    <br>
    <tr>
      <p>How often do you perform the task? (Choose one)</p></tr>
    <tr>
      <p><input type="text" style="width: 3em;" id="day">&nbsp;x Per day&nbsp;
        <input type="text" style="width: 3em;" id="week">&nbsp;x Per week&nbsp;
        <input type="text" style="width: 3em;" id="month">&nbsp;x Per month&nbsp;
        <input type="text" style="width: 3em;" id="year">&nbsp;x Per year&nbsp;
      </p>
    </tr>
    <br>
    <tr>
      <p><input type="submit" value="Submit" id="submitButton"></p>
    </tr>

    <br>
    <tr>
      <p id="result"></p>
    </tr>
  </table>
</form>

The jQuery function that isn't working so far is this...

function calcTime() {
var seconds = $('#seconds');
var minutes = $('#minutes');
var perDay = $('#day');
var perWeek = $('#week');
var perMonth = $('#month');
var perYear = $('#year');
if ((seconds.value > 0) && (minutes.value > 0)) {
alert('Please only choose either seconds or minutes!');
return false;
  }
}

Thanks

2 Answers 2

1

Use jquery method val to retrieve elements value.

seconds.val()

If you do not use jquery to select element, but pure Javascript

var seconds = document.getElementById('seconds')

you will be able use value property seconds.value.

Jquery select returns 'it's own' object not native HTMLElement.

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

1 Comment

ah, thank you. I knew it was going to be a stupid little oversight
1

Using jQuery you can change your 'if' statement to:

if (seconds.val() > 0 && minutes.val() > 0)

More info about the .val() method: http://api.jquery.com/val/

Comments

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.