1

I want to perform an if statement based upon the value of a hidden input within myform.

the scenario is that I have a table consisting of tests. Each test is essentially its own form. the hidden input basically contains 'YES' if the test has a password attached. I want to prompt the user to enter the password if that is the case when they attempt to execute a particular test (submit the form).

so here is my code I am attempting:

$("#submit").live('click', function(event) {
    if ($(this).parent().find(':input:hidden[name=has_password]').val() == 'YES') {
            $('.messagepop').remove();
                $(this).parent().append('<div class="messagepop pop"><p><label for="password">Enter test password</label><input type="text" size="30" name="pass" id="pass" /></p><p><input type="button" id="submitPass" value="GO" id="pass_submit"/> or <a class="close" href="/">Cancel</a></p></div>');
                $(".pop").slideFadeToggle(function() { 
                $("#pass").focus();
                });
                return false;
    }  
});

Without the if statement the code works: on clicking a particular button within a form, another small password entry form pops up. However I only want the form to pop up if a value of a hidden input is 'YES'.

I have checked that values are present in the hidden input.

I imagine I am using the jQuery slightly wrong as I am a beginner. So can somebody identify a better way of performing the check?

Many thanks,

EDIT (jsfiddle with html etc):

jsfiddle

9
  • Your code seems fine. We need more context to find the actual problem. It would be best if you can recreate the scenario in jsfiddle. Commented Apr 27, 2011 at 14:37
  • Hi brian, As far as I can see it, this is essentially what I have: jsfiddle.net/ezFgU/11 and it works on jsfiddle :P Commented Apr 27, 2011 at 14:43
  • @brianpeiris how is his code fine the selector is incorrect Commented Apr 27, 2011 at 14:44
  • @buymypies what you show as html in your post is not valid html and doesn't match what is in your fiddle can we see the html source Commented Apr 27, 2011 at 14:47
  • 1
    @buymypies your js fiddle still has invalid html get that sorted out first. the form needs to be in a td the items in the form should not be without a parent in the form every submit button needs a different id Commented Apr 27, 2011 at 15:01

3 Answers 3

2

Your HTML is extremely malformed. The <tr> and <form> end-tags are mismatched, you've got a <form> tag directly inside the <tr>, which is not allowed, you haven't self-closed your <input /> tags and you have duplicate id's on the same page.

<tr>
  <form name='myform' method='post' action='test_sim.php'>
    <input name='test' type='hidden' value='1'>
    <input type='hidden' name='has_password' value='NO'>
    <td>TEST 1</td>
    <td>B352</td>
    <td>10</td>
    <td>2011-05-12 06:00:00</td>
    <td>2011-05-12 12:00:00</td>
    <td>06:00:00</td>
    <td><input id='submit' type='button' value='execute test'></td>
  </tr>
</form>

It should be like this:

<tr>
  <td>TEST 1</td>
  <td>B352</td>
  <td>10</td>
  <td>2011-05-12 06:00:00</td>
  <td>2011-05-12 12:00:00</td>
  <td>06:00:00</td>
  <td>
    <form name='myform' method='post' action='test_sim.php'>
      <input name='test' type='hidden' value='1' />
      <input type='hidden' name='has_password' value='NO' />
      <input class='submit' type='button' value='execute test' />
    </form>
  </td>
</tr>

Fixing the HTML solves your problem: http://jsfiddle.net/brianpeiris/ezFgU/16/

You should use the W3C Validator tool to check your HTML so that you can prevent these types of issues.

Edit: As mcgrailm mentioned, your id attributes are duplicated. jQuery is forgiving here, it seems, that's why the above jsFiddle works. You should use a class to identify your submit buttons and use the corresponding $('.submit') selector to attach a click handler.

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

3 Comments

thank you! it seems I've learnt a lot from such a small task.
please note that you should not leave inputs in the from without a parent to have valid html so you should put them in divs or ul->li
@mcgrailm Only if you're aiming for Strict validation, I think. The HTML5 spec allows it.
1

i believe

if ($(this).parent().find(':input:hidden[name=has_password]').val() == 'YES')

should be

 if ($(this).parent().find('input[name=has_password]:hidden').val() == 'YES')

Comments

1

i think your selector is incorrect try this

$("#submit").live('click', function(event) {
    if ($(this).parent().find('input[name="has_password"]:hidden').val() == 'YES') {
            $('.messagepop').remove();
                $(this).parent().append('<div class="messagepop pop"><p><label for="password">Enter test password</label><input type="text" size="30" name="pass" id="pass" /></p><p><input type="button" id="submitPass" value="GO" id="pass_submit"/> or <a class="close" href="/">Cancel</a></p></div>');
                $(".pop").slideFadeToggle(function() { 
                $("#pass").focus();
                });
                return false;
    }  
});

WORKING DEMO

5 Comments

ye, I have tried it. I just get nothing happening on the clicking #submit now.
I don't understand why its not working :( Must be something to do with having multiple forms...but they are all seperate so I am lost
can you post the html also if you are using an actual submit button you will need to prevent the default I updated my fiddle to show how to do that
Can you give your forms different identifiers?
@PMV i've removed form id's that was jst there coz i was trying a different approach.

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.