Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 887223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:11:50+00:00 2026-05-15T13:11:50+00:00

While having one of my questions answered, cletus mentioned that when creating elements in

  • 0

While having one of my questions answered, cletus mentioned that when creating elements in jQuery it’s better to use direct DOM element creation, instead of innerHTML. I tried googling it but I wasn’t able to find a good article with comparisons.

I’ve provided this code bellow as an example and I was wondering if someone could help me rewrite it in direct DOM element creation form in hope that i would also learn the difference afterwards.

var img = $(this);
img.append('<p class="cap"><a href="'+img.parent().attr('href')+'">'+
img.attr('title')+'</p></a>');

Thanks so much.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T13:11:51+00:00Added an answer on May 15, 2026 at 1:11 pm

    Yeah, avoid HTML string-slinging. You’re giving yourself bugs and potentially cross-site scripting security holes. For example:

    '<a href="'+img.parent().attr('href')+'">'
    

    what if the URL in the parent’s href contains characters that are special in this HTML context, like <, " or &? At best you’ll get invalid markup that might trip the browser up; at worst, if the URL comes from user submission, a security problem.

    (OK, < and " are unlikely to appear in a URL, but & is very likely indeed. The title you’re putting in the text content may be more vulnerable.)

    This is a large and increasing problem. Just as we’re starting to get a handle on fixing the HTML-injection bugs coming from templating on the server-side (eg. PHP users forgetting to htmlspecialchars()), we’re now introducing loads of new client-side XSS from naïve HTML-hacking with innerHTML and jQuery’s html(). Avoid.

    You can escape text you’re inserting into HTML manually:

    function encodeHTML(s) {
        return s.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/"/g, '&quot;');
    }
    
    '<a href="'+encodeHTML(img.parent().attr('href'))+'">'
    

    but really, you’re better off using the direct DOM-style properties and methods, which, since they involve no HTML parsing, don’t require any escaping. They’re called DOM-style because that’s the way the original DOM Level 1 HTML features work (which existed back before innerHTML reared its ugly head):

    var a= document.createElement('a');
    a.href= this.parentNode.href;
    a.appendChild(document.createTextNode(this.title));
    

    In jQuery you use attr() and text() to do the same:

    var a= $('<a></a>');
    a.attr('href', $(this).parent().attr('href');
    a.text($(this).attr('title'));
    

    And in jQuery 1.4, you can do it most conveniently using an element creation shortcut:

    $(this).append(
        $('<p class="cap"></p>').append(
            $('<a></a>', {
                href: $(this).parent().attr('href'),
                text: $(this).attr('title')
            })
        )
    );
    

    This still looks a bit questionable though, what’s the parent that you’re getting the href from? It’s invalid to put a <p>, or another link, inside a link.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that the web is full of questions like this one, but I
Having read Fowler's Refactoring for a while, I still often catch myself thinking I
Having been a PHP developer on LAMP servers for quite a while, is there
I'm having problems with my application receiving low memory warnings while the user is
I'm having trouble granting privileges to another user in PostgreSQL 8.3. While the GRANT
I am having huge lines data which could accomdate in Textbox. While I am
Edit: While this question has been asked and answered before ( 1 ), (
I realize that this question has been asked and has been answered here but
I've been having some 'strange' results while comparing dates. table1 has two rows with
Subquestioning [2] While copying Resource.mdf [1], I noticed that: 1) It is possible to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.