Second element in each loop returns undefined

<p>From fields with the class <code>.edit-links</code> i´m able to retrieve the values but <code>$(this).next(".edit-url").val();</code> only returns the first found element then only returns undefined. In the console i can see that "this" is targeting the first element as expected but the second .edit-url is not found and returning prevObject.</p><div class="snippet" data-lang="js" data-hide="false"><div class="snippet-code"><pre class="snippet-code-js lang-js prettyprint-override"><br /><code>$('#edit').click(function() {<br /> edit.revertLink();<br />});<br /><br />var edit = {};<br />edit.revertLink = function() {<br /><br /> $('.edit-link').each(function(index) {<br /><br /> var currentName = $(this).val();<br /> var url = $(this).attr('data-tempurl');<br /> var meta = $(this).attr('data-meta');<br /> //edit.changeLiOrgname(this, currentName);<br /><br /> if (meta === "external") {<br /> //url = $(this).next(".edit-url").val();<br /> currentName = $(this).next(".edit-url").val();<br /> //edit.changeLiOrgURL(this, url);<br /> $(".edit-url").remove();<br /> }<br /><br /> var linkStr = [<br /> '<a href="' + url + '" class="aktiv">',<br /> '' + currentName + '',<br /> '<span class="type float-r">',<br /> '' + meta + '',<br /> '</span>',<br /> '</a>'<br /> ];<br /><br /> $(".removeBtn").remove();<br /> $(this).replaceWith(linkStr.join(''));<br /><br /> });<br />}</code><br /></pre><pre class="snippet-code-html lang-html prettyprint-override"><br /><code><button id="edit">Edit</button><br /><li data-identifier="4.3e43f44714869abce3fabc5"><br /> <input class="edit-link" data-meta="internal" data-tempurl="/internal-page.html" type="text" value="Stöd"><br /></li><br /><li><br /> <input class="edit-link" data-meta="external" type="text" value="Link"><br /> <input class="edit-url" type="text" data-tempurl="www.link.se" value="www.link.se"><br /></li><br /><li><br /> <input class="edit-link" data-meta="external" type="text" value="link2"><br /> <input class="edit-url" type="text" data-tempurl="www.link2.se" value="www.link2.se"><br /></li><br /><li><br /> <input class="edit-link" data-meta="external" type="text" value="link3"><br /> <input class="edit-url" type="text" data-tempurl="www.lin3k.se" value="www.link3.se"><br /></li></code><br /></pre></div></div><br /><h2>Click here view more answers <a href="%20http://stackoverflow.com/questions/34525875/second-element-in-each-loop-returns-undefined">http://ift.tt/1Jfjeva</a></h2>

Nhận xét

Bài đăng phổ biến từ blog này

Align div to bottom of the page in bootstrap

Align elements inside an tag vertically

Javascript is unable to stop page loading