0

I am trying to store multidimensional array, taken from an HTML form, into javascript array.

I have this simple form:

        <label for="radek_predpisu_1">První řádek předpisu</label>
        <input type="text" name="predpisy[text][]" id="text_predpisu_1" value="" maxlength="100" size="60" title="Text předpisu" placeholder="Text předpisu" class="text ui-widget-content ui-corner-all">
        <input type="text" name="predpisy[cena][]" id="cena_predpisu_1" value="" maxlength="4" size="4" title="Cena bez DPH" placeholder="Cena bez DPH" class="text ui-widget-content ui-corner-all">
        <input type="text" name="predpisy[pocet][]" id="pocet_predpisu_1" value="" maxlength="4" size="4" title="Počet jednotek" placeholder="Počet jednotek" class="text ui-widget-content ui-corner-all">

Now I use:

var predpisy = $('[name="predpisy"]');

What should I do next? If I try to alert( predpisy['text'][0] and I got undefined.

Thank you very much.

Radek

2 Answers 2

2

$('[name="predpisy"]') is only going to give you elements whose name exactly matches the string 'predpisy'.

What you want is this: $('[name*="predpisy"]'), which will select all elements whose name contains 'predpisy', which I think is what you're looking for.

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

3 Comments

Yes, thank you, I didnt know it, but it still returns undefined.
I see what you're trying to do now. I'm not sure jQuery works that way that you expect it to. You can try something like $('[name*="predpisy[text][]"]')[0]
$('[name*="predpisy"]') will give you an array of your text fields, but you will have to reference them by their index, e.g.: var predpisy = $('[name*="predpisy"]') alert(predpisy[0])
0
var predpisy = $('[name^="predpisy"]');
console.log(predpisy[0]['text']);
console.log(predpisy[0]['cena']);
console.log(predpisy[0]['pocet']);

5 Comments

This is, what I am trying too - I got undefined also.
Updated. try once.
Still the same, but I think, that should be predpisy['text'][0] , but it also doesnt work.
what is the output for console.log(predpisy)
Object { length: 15, prevObject: Object, context: HTMLDocument → index.php, selector: "[name^="predpisy"]", 15 more… }

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.