Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    zc5
    zc5 is offline
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Removing linked text while using Jquery script

    I am trying to do a very simple task but I cannot seem to figure it out. I am trying to write a function that will remove a link without removing the text associated with it. Here are the specifics:


    Code:
    <a href="../../../../articles/c/a/t/Special%7ECategories_101d.html" title="Special:Categories">Categories</a>
    I am trying to remove everything EXCEPT the text "Categories", I just want to remove the href tag. I would even be willing to rename the title "Categories". I just want to link gone. This link is in a div called "mw-normal-catlinks" and it is the first link in the div. I do not want to remove the other content in that div.

    I am very new to Jquery and so far I have an idea of what to do.. this is what i got:

    Code:
    <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    
    $('#mw-normal-catlinks a.first').replaceWith('Categories');
    </script>
    Now I am not sure if I am missing anything because when i put this in my HTML code, nothing happens. I'm not sure if I need to add a div to make it work.
    I want to eventually implement this into a php coded tool i use to convert wiki pages to html so maybe I will need to add some sort of div?

    Thanks everyone!

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,348
    Thanks
    23
    Thanked 618 Times in 617 Posts
    If There are no other links with title="Special:Categories" you can use this:
    Code:
    $('[title="Special:Categories"]').replaceWith($('[title="Special:Categories"]').text());
    Else use this:
    Code:
    $('div#mw-normal-catlinks a:first-child').replaceWith($('div#mw-normal-catlinks a:first-child').text());
    Last edited by sunfighter; 07-31-2013 at 02:49 PM.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #3
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    Quote Originally Posted by sunfighter View Post
    If There are no other links with title="Special:Categories" you can use this:
    Code:
    $('[title="Special:Categories"]').replaceWith($('[title="Special:Categories"]').text());
    Else use this:
    Code:
    $('div#mw-normal-catlinks a:first-child').replaceWith($('div#mw-normal-catlinks a:first-child').text());
    the second example is much more effcient btw. the first one will check every element in the dom. If yo uhave to go that way at least specify the element

    Code:
    $('a[title="Special:Categories"]').replaceWith($('a[title="Special:Categories"]').text());

  • Users who have thanked DanInMa for this post:

    sunfighter (07-31-2013)

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Alternative solution that removes just the href.

    Code:
    $('#mw-normal-catlinks a:first-child').removeAttr('href');
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •