0

I've created a JavaScript function that checks if a certain data already exists in my database. What I want to know is if there is a way to make the input field name in a JavaScript pass as an argument

Here is my code

    function checkDataAvailability(displayid,input_id,fieldname)
    {
        'use strict';
        $(document).ready(function(){
            //var x = document.getElementByName(fieldname).elements;
            $(displayid).load('php/signcheck.php').show();
            $(input_id).keyup(function(){
                 },
                $.post('php/signcheck.php', { username: form.x.value }, 
              //$.post('php/signcheck.php', { username: form.fieldName.value }, 
                function(result){
                    $(displayid).html(result).show();
                });
            });

        });
    }
    var a =  checkDataAvailability ('#userstat','#username_input','username');

A little explanation. The two commented lines are the two methods I've tried to run the field name as an argument separately. Unfortunately they aren't working.

Here is my form

<form action="php/register_exec.php" method="POST" name="form">
Username <span id="userstat" class="checkerr"></span>
<input type="text" name="username" id="username_input" required>
</form>
2
  • document.getElementById('username_input').value Commented Jun 5, 2016 at 3:39
  • you can pass it as var a = checkDataAvailability ('userstat','username_input','username'); Commented Jun 5, 2016 at 3:42

3 Answers 3

1

Passing form fieldnames as argument is no different than passing string argument to functions

var a = checkDataAvailability ('userstat','username_input','username');

Important thing is what you do inside the function.

You can get the value of input field in primarily two ways

  1. Directly read the value using value property as:

    document.getElementById('username_input').value
    

    or

    document.getElementById(fieldid).value //if you pass fieldid to your function
    
  2. Use the form field directly

    //assuming you pass formname and fieldname as variables to your function
    var form = document.getElementById(formname);
    var inputvalue = form.elements.namedItems(fieldname).value
    

You can modify them to suit your jquery syntax if need be.

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

Comments

0

Since you're already using the jQuery library, you can continue using it.

$('input[name="' + fieldname + '"]').val()

Comments

0

There are 3 ways of achieving what you desire -

  • If you want to stick with your current code pattern, then replacing form.fieldname with form[fieldname] would get you the correct results. This is because fieldname is a string, and form."some string" would give you an error.
  • The other two ways are the same as neouser99 and avck specified in their answers.

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.