jquery mobile submit button conditional appearance

I made an attempt to create a form in wich the submit form will appear only i a condition was met (the sum of the boxes should be equal to 100). Any ideas?

<!DOCTYPE html>
<html>
<head>
  <!-- Include meta tag to ensure proper rendering and touch zooming -->
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <!-- Include jQuery Mobile stylesheets -->
  <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
  <!-- Include the jQuery library -->
  <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
  <!-- Include the jQuery Mobile library -->
  <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>

<body>

<div data-role="page">

<div data-role="header" data-theme="b">
    <h1>title</h1>
</div>


<div data-role="content">
    <fieldset>
	
	<form action="#.asp" name="form1" method="post" data-ajax="false" class="ui-body ui-body-a ui-corner-all">

	
	
		<fieldset data-role="controlgroup">
		<legend>Cake</legend>
		
		<table width="100%" border="0" style="margin-right:10%;">
			<tr valign="top">
				<td id="cakeitemscontainer">
					<table cellpadding="3">
					
					
						<tr class="cakeitems">
							<td>Answer 1</td>
							<td><input type="number" id="q8_1" name="q8_1" value="" max="100" onChange="update1()"  /></td>
						</tr>
					
						<tr class="cakeitems">
							<td>Answer 2</td>
							<td><input type="number" id="q8_2" name="q8_2" value="" max="100" onChange="update1()" /></td>
						</tr>
					
									
						<tr height="20"><td></td></tr>
						<tr id="sumcake"><td>Total:</td><td><input type="number" value="0" max="100" name="sum" id="sum" size="5" disabled />
						(Should sum up to 100)
						</td></tr>
					</table>
				</td>
			</tr>
		</table>

	
	
		<input type="submit" data-theme="b" value="submit" id="submitButton" name="continue">
			
	</form>
		
		
		<script>
		$("form1").validate();
		</script>
		
</fieldset>
</div>
</div>

<script type="text/javascript">
<!--
    function update1() 
	{
	var sum = 0;
	for (var j=0; j<document.form1.elements.length; j++)
		{
		if (document.form1.elements[j].type == 'number' && document.form1.elements[j].value != '' && document.form1.elements[j].id !='sum')
			{
			sum = sum + parseFloat(document.form1.elements[j].value);
			}
		}
	document.form1.sum.value = sum;
	if (sum == 100) 
		{document.getElementById("submitButton").style.visibility = "visible";} 
	else
		{document.getElementById("submitButton").style.visibility = "none";}
	}
//-->
</script>

</body>
</html>

Open in new window

arielbfAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Firstly you need to clean up your code you have some mismatching <fieldset> tags in your markup.

Second I would use jQuery event handlers to do the work rather than specifying onchange attributes in your inputs.

Third jQuery mobile wraps buttons in a div and hides the actual button - so we have to use the target button as the closest jQuery mobile wrapper div. The code below demonstrates how to do this.

A working sample can be found here
Source cod on sample page and below
<!DOCTYPE html>
<html>
<head>
  <!-- Include meta tag to ensure proper rendering and touch zooming -->
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <!-- Include jQuery Mobile stylesheets -->
  <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
  <!-- Include the jQuery library -->
  <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
  <!-- Include the jQuery Mobile library -->
  <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>

<body>
<div data-role="content">
  <div data-role="page">
    <div data-role="header" data-theme="b">
      <h1>title</h1>
    </div>
   <form action="#.asp" name="form1" method="post" data-ajax="false" class="ui-body ui-body-a ui-corner-all">
      <fieldset data-role="controlgroup">
        <legend>Cake</legend>
        <table width="100%" border="0" style="margin-right:10%;">
          <tr valign="top">
            <td id="cakeitemscontainer">
              <table cellpadding="3">
                <tr class="cakeitems">
                  <td>Answer 1</td>
                  <td><input type="number" id="q8_1" name="q8_1" value="" max="100" /></td>
                </tr>
                <tr class="cakeitems">
                  <td>Answer 2</td>
                  <td><input type="number" id="q8_2" name="q8_2" value="" max="100" /></td>
                </tr>
                <tr height="20"><td></td></tr>
                <tr id="sumcake"><td>Total:</td><td><input type="number" value="0" max="100" name="sum" id="sum" size="5" disabled />
                (Should sum up to 100)
                </td></tr>
              </table>
            </td>
          </tr>
        </table>
        <input type="submit" class="fred" value="submit" id="submitButton" name="continue" />
      </fieldset>
    </form>
  </div>
</div>
<script type="text/javascript">
$(function() {
  // Hide the jQuery Mobile button
  $('#submitButton').closest('.ui-btn').hide();
  // Look for a change on inputs that start with "q8"
  $('input[name^="q8"]').change(function() {
    var sum = 0;
	// Loop through all inputs with names that start
	// with "q8" and sum their values
    $('input[name^="q8"]').each(function() {
      sum+= parseInt($(this).val());
    });
	// If the sum == 100 then show the button otherwise
	// hide it
    if (sum == 100) {
      $('#submitButton').closest('.ui-btn').show();
    }
    else {
      $('#submitButton').closest('.ui-btn').hide();
    }
  });
});
</script>

Open in new window

0
arielbfAuthor Commented:
Seems pretty good but in my code the input "sum" was shown as people filled the 2 "Answers" and now you don't see it (that was the the all point of function 'update1'). Can you fix that?
0
Julian HansenCommented:
Yup, just add
$('#sum').val(sum)

Open in new window

before the check for == 100

Sample code updated
<script type="text/javascript">
$(function() {
  // Hide the jQuery Mobile button
  $('#submitButton').closest('.ui-btn').hide();
  // Look for a change on inputs that start with "q8"
  $('input[name^="q8"]').change(function() {
    var sum = 0;
    // Loop through all inputs with names that start
    // with "q8" and sum their values
    $('input[name^="q8"]').each(function() {
      sum+= parseInt($(this).val());
    });
  
    // Set the sum
    $('#sum').val(sum);

    // If the sum == 100 then show the button otherwise
    // hide it
    if (sum == 100) {
      $('#submitButton').closest('.ui-btn').show();
    }
    else {
      $('#submitButton').closest('.ui-btn').hide();
    }
  });
});
</script>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
arielbfAuthor Commented:
Great, Thank you!
0
Julian HansenCommented:
You are welcome.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.