6076

jQuery: trying to create “slug”, target anchors

Question:

Creating a Slug

Title variable which holds name of the article "Article X".

$("div.article h2").each(function(){ var title = $(this).text();

Attempt to create slug with trim, lowercase, and replace methods

var slug = title.trim().toLowerCase().replace(" ", "_");

Attemp to Insert the Target Anchors

var target_anchor = "<a name='" + slug + "'/>";

This is where I got CONFUSED in the Instructions:

Then, on your own, use the append method to stick this inside the h2. Remember that self is a pure Javascript object and it doesn’t have an append method. Link to the Targets

We need to add links into the list_item. On your own, work with the var list_item= line to include a link tag where the href points to #article_x where article_x is the current slug.

How do I write this?

My attempt did not work:

var list_item = "<li>" + "<a href='#article_x'</a> " + slug + "</li>"

Answer1:

You should do

$("div.article h2").each(function(){ var title = $(this).text(); var slug = title.trim().toLowerCase().replace(" ", "_"); var list_item = "<li><a href='#"+slug+"'> " + title + "</a></li>"; $(this).prop('id', slug); $(WHERE_YOU_WANT_TO_APPEND_IT).append(list_item); }

Change WHERE_YOU_WANT_TO_APPEND_IT by your selector

Recommend

  • Filter specific Message with logstash before sending to ElasticSearch
  • GetSaveFileName() how to update the file extension in the “File name:” control?
  • iCal: How do I delete an .ics file that has subscriptions?
  • Deserializing a collection of interfaces
  • change ImageView Resource in a ListView Layout from an AsyncTask that load contactsList
  • Linq (GroupBy and Sum) over List
  • Default file for Apache localhost
  • How does facebook select image to display on timeline
  • jQuery generic code
  • How to reject an email using mailgun?
  • Unable to start the virtual device Details: failed to create framebuffer image (error: 15)
  • Java program and mySQL connectivity issue: No suitable driver found
  • Solrj indexing mechanism
  • Solrj indexing mechanism
  • Disable generics validation in Eclipse via M2E
  • JavaScript RegEx to match punctuation NOT part of any HTML tags
  • How do I use DisplayFor() for objects that are not directly part of my model in asp .net mvc 2?
  • Can i run WebAppContext with WebSocketHandler in same instance?
  • Tornado non-blocking SMTP client
  • Increasing dimensions on hover without changing the position of other elements
  • How to make Solr's spell checker ignore case?
  • Yii - Make a string usable in a URL or filename
  • How to do custom filtering in Datatables with comma separated values?
  • How to clear Firefox' console via javascript?
  • Excel 2007: Format of email address from Outlook 2007
  • Updating and removing unique join relationships in CakePHP
  • Negating Regex PO BOX
  • How does this usort cmp function actually work?
  • Admob requires api-13 or later can I not deploy on old API-8 phones?
  • Checking free space on FTP server
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • PHP - How to update data to MySQL when click a radio button
  • Join two tables and save into third-sql
  • How to handle AllServersUnavailable Exception
  • R: gsub and capture
  • AT Commands to Send SMS not working in Windows 8.1
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?