#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    113
    Rep Power
    14

    Question Remove/trim whitespace from <a name=" here ">


    Hi, I have a page with several anchors that are dynamically created, and for some reason adds spaces around the dynamically created name value. For a temporary fix I was just trying to remove the spaces with jQuery. The HTML is in the format as:
    Code:
    <span class="linkAnchor"><a name=" dynamicanchorname "></a></span>
    Should be:
    Code:
    <span class="linkAnchor"><a name="dynamicanchorname"></a></span>
    I've been trying different variations of trim(); and such but with no luck. Could please get pushed in the right direction on how to remove the leading and trailing spaces around the anchor name?

    Thanks!
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1016
    Hi,

    why on earth is there a name attribute, anyway? This comes from the early days of HTML and is long obsolete. Nowadays, we use the generic id attribute.

    So instead of wasting your time with silly workarounds, I suggest you fix the markup: Replace the attribute, remove the superfluous span element (what do you need it for?) and get rid of the spaces. If you need help with that, just post the code in the right forum.

    Comments on this post

    • kicken agrees
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    113
    Rep Power
    14
    Thanks. The value within the "name" is created dynamically by a 3rd party component, I searched through their code and couldn't find where that was. Besides, I would have to edit their core code which I prefer not to do. I can change the name to id, I see where that is, no problem there, but I don't have control over the spaces that are added around the value. I put the span there so that I could isolate those anchors only.

    With using ID, this is the code I needed spaces removed from:
    Code:
    <span class="linkAnchor"><a id=" dynamicanchorname "></a></span>
    Thanks again.
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1016
    I understand your goal, but it simply doesn't work like that. You cannot change the markup with JavaScript, because the people who don't have it turned on will still be getting the original, broken page. That means no reference to the trimmed IDs will work for them.

    If you don't know how to fix the bug yourself, get in touch with the third party. That's really all you can do.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    113
    Rep Power
    14
    Cool, makes sense. I'll submit a bug report (which I view this as) to the 3rd party.

    Thanks.

    Comments on this post

    • Jacques1 agrees : Cool. :-)
  10. #6
  11. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,288
    Rep Power
    188
    Although I can see what Jacques1 is saying and would haft to agree that markup is very important. That being said..., if your "a" tag is built from a JavaScript based plugin; I can see how having control over the plugin's automatically assigned tag attributes could present a dilemma and issue with markup validation (based on current standards). The .trim() API will give you a fix for this attribute and replacing the "name" attribute with "id" attribute would be valid markup. Because..., if the end users computer has JavaScript disabled; then the "a" tag is not going to be even created. As I think the "a" tag is being created dynamically via jQuery... correct? If it is; here is what you could do... trigger the code below after the plugin loads (after the plugin builds the "a" tags).
    Code:
    $("a").each(function(e){
    	$("a").eq(e).attr("id",$(this).attr("name"));
    	$("a").removeAttr("name");
    });
    		$("a").attr("id").trim();
    Last edited by web_loone08; March 30th, 2014 at 01:11 PM.

IMN logo majestic logo threadwatch logo seochat tools logo