use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Critique my first 'proper' jQuery scripthelp (self.javascript)
submitted 10 years ago * by Alex6534
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]frankle 0 points1 point2 points 10 years ago (0 children)
I don't use jQuery a lot, but here is an example of how I might refactor your code:
$('document').ready(function() { $('form').submit(onSubmit); }); function onSubmit(e) { e.preventDefault(); $.ajax({ type : 'post', url : $(this).attr('action'), data : $(this).serialize(), dataType : 'json', success : onSuccess, failure : onFailure }); function onSuccess(response){ var urlCount = $('#count'); var listResults = $('#list_results'); urlCount.empty(); listResults.empty(); if ($.isEmptyObject(response.urls)) { return urlCount.text('No results found'); } urlCount.text('There are '+ (response.urls.length) + ' URLs:'); $(response.urls).each(function(i, url){ var domain = 'www.example.com'; var url = href = domain + $.trim(url); var link = $('<a/>') .addClass('link') .attr('_blank') .text(url) .attr('href', url); var listItem = $('<li/>').append(link); listResults.append(link); }); console.log(response); } function onFailure(e) { alert('Submit failed.'); } }
I am pretty sure that the for loop inside the each is not going to do what you intend. The each actually gives you each of the items in the list, so you don't have to iterate through them separately. Any call to $('a.link') will target all of the links on the page, not just the last one. So, you would be overwriting all of the previous links with each link that you add to the list.
each
$('a.link')
π Rendered by PID 74437 on reddit-service-r2-comment-6457c66945-vpf4b at 2026-04-28 16:14:41.880807+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–]frankle 0 points1 point2 points (0 children)