1

I have a form where you can add field by clicking an add-button to fill them with data in the first step. In the second step i'd like to get this data displayed but my loop doesn't correctly.

This is the origin table. By clicking the add-button a new tr-tag with new IDs (item2, Ditem2...) is generated.

<table cellpadding="0" cellspacing="0" class="table" id="rp-article">
    <tbody>
        <tr class="rowb">
            <td class="col1 cell">
                <input disabled="disabled" id="count1" maxlength="10" name="count1" size="1"
                type="text" value="1" />
            </td>
            <td class="col2 cell">
                <input id="item1" maxlength="100" name="item1" size="15" type="text" value="Artikelnummer"
                />
            </td>
            <td class="col3 cell">
                <input id="Ditem1" maxlength="100" name="Ditem1" size="48" type="text"
                value="Beschreibung..." />
            </td>
            <td class="col4 cell">
                <input id="Aitem1" maxlength="100" name="Aitem1" size="5" type="text"
                value="Menge" />
            </td>
            <td class="col5 cell">
                <input id="Pitem1" maxlength="100" name="Pitem1" size="10" type="text"
                value="Preis" />
            </td>
            <td class="col6 cell">
                <select id="Ritem1" name="Ritem1">
                    <option>Retourengr&uuml;nde:</option>
                    <option>Ware falsch geliefert / falscher Inhalt</option>
                    <option>Ware falsch bestellt</option>
                    <option>Ware gef&auml;llt nicht</option>
                    <option>Ware passt nicht</option>
                    <option>Ware doppelt geliefert</option>
                    <option>Sonstiges</option>
                    <option>Ware defekt/besch&auml;digt/fehlerhaft</option>
                </select>
        </tr>
    </tbody>
</table>

Now I'd like do grab them all and display them in p-Tags (n is the count variable for the amount of tr):

for (var x = 1; x = n; x++) {
  $('#pt_title2').after('<p id=#pt_article' + x'></p>');
  $('#pt_article'+x).html('<b>#' + x'</b>'); 
}
4
  • Your loop for (var x = 1; x = n; x++) needs a comparison, atm you are assigning the value of n to x, should read: for (var x = 1; x == n; x++) Commented Sep 27, 2012 at 8:03
  • @m90 thats a pretty short loop.... should probably be x < n. Commented Sep 27, 2012 at 8:08
  • right. thank you. but there is a "Unexpected token" error in this loop. don't know why. Commented Sep 27, 2012 at 8:09
  • @AndersHolmström That's so true.... Commented Sep 27, 2012 at 8:16

1 Answer 1

1

Your for loop is not correct. Try this instead:

for (var x = 1; x < n; x++) { // <--- note the < instead of =
  $('#pt_title2').after('<p id=#pt_article' + x + '></p>'); // you were missing a +
  $('#pt_article'+x).html('<b>#' + x + '</b>');  // same here
}

Now the loop will run for as long as x is smaller than n... which I think you want.

Possibly you could need to use <= (less than or equal to), if you want the loop to run until x has the same value as n. Depends on how you have the count, ids etc set up for your elements.

The for loop condition isn't "run until this is true", it's "run while this is true".

You should also possibly check out jQuery's each function.

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

1 Comment

That's great. Please accept and upvote my answer if it was correct, so that others can see it in the future. (And also I want reputation points ;))

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.