0

I have a list of form elements that I want to loop over to get the values of, so if someone typed their name in the input i want their name, if they selected an option from a select box I want the not the numerical value but the string. All these values needs to be outputted as one string.

This is the loop i've created, I however have no idea how to go about this problem..

every form element has a name starting with credit_

if someone could point me in the right direction it would be much appreciated..

$(this).parent().parent().find('[name*=credit_]').each(function( index ){

});

my html is quite simple.

<div class="comp-row">
     <!-- a select -->
     <!-- an input -->
</div>

This is part of the form, there are many other form fields but im only concerned with the ones within "comp-row" which Im manipulating a lot.

I ended up using:

$('.comp-row [name*="credit_"]:not([type=hidden])')
.each(function(index,elem)
{
    console.log($(this).text() != '' ? $(this).find('option:selected').text().trim() : $(this).val());
});
}
3
  • 2
    What does your HTML look like? Commented Nov 27, 2013 at 17:31
  • I agree with @qwertynl, it would be useful to see this. Could you maybe set up a jsFiddle or similar as a starting point? Commented Nov 27, 2013 at 17:35
  • you only set the name for the select and the input right? name property is invalid for option tag. Commented Nov 27, 2013 at 18:04

4 Answers 4

1

Youre looking for the $('select[name*="credit_"]>option:selected') selector.

To read the text value for your , issue .text()

Combine this with if($('input[name*="credit_"]').text() != '') evaluation, combined something like this:

var theName = $('input[name*="credit_"]').text() != '' 
   ? $('select[name*="credit_"]>option:selected').text() 
   : $('input[name*="credit_"]').text();
Sign up to request clarification or add additional context in comments.

Comments

1

Depending on format you want you can use serialize() or serializeArray().

For example to obtain for whole form:

var data=$('#myForm').serialize()

For specific group of elements:

$('[name*=credit_]').serializeArray()

serialize() API docs

serializeArray() API docs

2 Comments

ah no this is part of a form, there are many other form fields within the form but im concerned with only a section.
.serialize will get the value of a select option, he wants the text of it.
1
var result = '';
$(this).parent().parent().find('[name*=credit_]').each(function( index ){
    result += $(this).is("select") ? $(this).text() : $(this).val();
});

Comments

0

Iterate over all elements that match your criteria (name*=credit_). Check its type and put the value inside a variable.

HTML

<form>
    <input type="text" name="credit_a" value="1" />
    <input type="text" name="credit_b" value="2" />
    <input type="text" name="credit_c" value="3" />
    <select name="credit_d">
        <option value="kk">kk</option>
        <option value="jj" selected>jjjjj</option>
    </select>
    <input name="credit_e type="checkbox" checked value="imchecked" />
</form>

<form>
    <input type="text" name="credit_a" value="55" />
    <input type="text" name="credit_b" value="66" />
    <input type="text" name="credit_c" value="77" />
    <input type="text" name="credit_d" value="88" />
</form>

<p id="result"> </p>

javascript

$(function() {
    var values = '';

    $('form [name*=credit_]').each(function() {
        var $this = $(this);

        if($this[0].tagName == 'TEXTAREA') {
            values += ' ' + $this.text();   
        } 
        else if ($this[0].tagName == 'SELECT') {
            values += ' ' + $this.find(':selected').text();
        }
        else {
            values += ' ' + $this.val();
        }   
    });

    $('#result').html(values);    
});

jsfiddle: http://jsfiddle.net/5tgzr/2/

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.