0

I was wondering if it possible to find an input by it's closest element? real code:

<form method="post" action="">
    <div class="rocket-form">
        <fieldset>
            <legend>Person Contacts</legend>
        </fieldset>

        <div class="rocket-label-element no-height">
            <dt id="id-label">&nbsp;</dt>
            <dd id="id-element">
                <input type="hidden" name="id" value="" readonly="readonly" id="id">
            </dd>
        </div>
        <div class="rocket-label-element no-height"><dt id="vendor_id-label">&nbsp;</dt>
            <dd id="vendor_id-element">
                <input type="hidden" name="vendor_id" value="" readonly="readonly" id="vendor_id">
            </dd>
        </div>
        <div class="rocket-label-element">
            <div id="firstname-label">
                <label for="firstname" class="rocket-label optional">First name</label>
            </div>
            <div class="rocket-element">
                <input type="text" name="firstname" id="firstname" value="any first name" maxlength="64" readonly="readonly">
            </div>
        </div>
        <div class="rocket-label-element">
            <div id="lastname-label">
                <label for="lastname" class="rocket-label optional">Last name</label>
            </div>
            <div class="rocket-element">
                <input type="text" name="lastname" id="lastname" value="any last name" maxlength="64" readonly="readonly">
            </div>
        </div>
        <div class="rocket-label-element">
            <div id="phone-label">
                <label for="phone" class="rocket-label optional">Phone</label>
            </div>
            <div class="rocket-element">
                <input type="text" name="phone" id="phone" value="0123456789" maxlength="32" readonly="readonly">
            </div>
        </div>
        <div class="rocket-label-element">
            <div id="email-label">
                <label for="email" class="rocket-label optional">Email</label>
            </div>
            <div class="rocket-element">
                <input type="text" name="email" id="email" value="[email protected]" maxlength="128" readonly="readonly">
            </div>
        </div>
    </div>
</form>

I have this form, and I want to get the value of the inputs in variables.. I tried to use "vendor_id" in order to use closest() or next() or prev() but no luck.. so, any help?

5 Answers 5

1

You can use

var vendorId = $("#vendor_id").val();

to get the value of vendor_id.

Be sure to include the # which identifies it as an id.

If you want to get the values of all the inputs, you can use:

$("input, select, textarea").each(function(){
    // Get value of inputs
});

If you have more than one form, you may use:

var formData = $(".rocket-form").closest("form").serialize();

Remember you will need to include jQuery with a script tag and you should wrap your code like so:

$(function() {
    console.log( "ready!" );
});

This ensures the page is ready before your JavaScript executes.

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

3 Comments

I need to get the values of <input(s) > related to #vendor_id, because I have another 4 forms in the page
Could you add an id or name?
unfortunately I can't
0

you can try this

var form = $("#vendor_id").closest("form").get();
$("form :input").each(function(){
 var input = $(this);
 var AllInputValues= $(input).val();
 alert (AllInputValues);

});

EDIT

var theVendform = $("#vendor_id").parent().closest("form").get();
                alert(theVendform["0"][3].defaultValue);
                alert(theVendform["0"][4].defaultValue);
                alert(theVendform["0"][5].defaultValue);
                alert(theVendform["0"][6].defaultValue);

1 Comment

great, thanks a lot, but it would be inappropriate to ask how to get them separated?
0

Not sure if I completely understand what you are asking but what about

$('#vendor_id-element input').val()

1 Comment

I need to get the values of <input(s) >
0

You a class in all your inputs where you want to get value, and do de following js

  Array.prototype.slice.apply(document.querySelector('.your-input-class')).forEach(function(el){
       Console.log(el.value);
    });

1 Comment

unfortunately I can't add classes
0

try this

var inputs = $('form').find('input');
//inputs is the array of your inputs and values can be accessed by:
var value1 = $(inputs[0]).val();

//or you can use for loop or forEach
for (var i = 0; i < inputs.length; i++) {
    var currentValue = $(inputs[i]).val();
    alert(currentValue);

}

6 Comments

great Idea, but unfortunately nothing happens
I was thinking about var elems = $("#vendor_id");; var elemsval = elems.parent().parent().nextAll('input').first().val(); but no luck
Have you checked your console for errors? The code seems to be working fine in my part. btw, I've updated the code
great, thanks a lot, but it would be inappropriate to ask how to get them separated?
this code will get me all forms in the file, right?
|

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.