views:

524

answers:

5

I have this html.

<a class="link" href="www.website.com?id=233253">test1</a>
<a class="link" href="www.website.com?id=456456">test2</a>

How can I hide one of these links by using the href attribute, and just the last numbers (233253), to hide the link with this href attribute and the class "link"?

This is not a working code, just something i put together to explain it better. getElementsByTagName('a').class('link').href="*233253"

Update: Unfortunately it has to be pure javascript, not using a library, and it has to work on IE6.

Update2: I don't have access to the html

+4  A: 

[edit]: code was somewhat sloppy, should work now. Including the split method (see comments).

Loop through the a elements, check href and apply the hiding. Like this:

var refs = document.getElementsByTagName('a');
for (var i=0;i<refs.length;i++) {
       if (
             refs[i].href &&
             refs[i].href.replace(/(\d+$)/,'$1').match('[your value to match]')
          ) {
           refs[i].className = refs[i].className.replace(/link/i,'');
           refs[i].style.display = 'none';
       }
}

OR

for (var i=0;i<refs.length;i++) {
   var hs = refs[i].href.split(/=/)[1];
   if (  hs.match([your value to match]) ) {
       refs[i].className = refs[i].className.replace(/link/i,'');
       refs[i].style.display = 'none';
   }
}
KooiInc
You can also split the href (refs[i].href.split(/=/)) and take element 1 of the resulting array to compare with your condition.
KooiInc
Is that a better solution. How would I do that with your code?
mofle
I couldn't get your script working. I get this error in Firebug. Firebug's log limit has been reached. %S entries not shown. Preferences missing ; after for-loop initializer[Break on this error] for (var i=0,i<refs.length;i++) {\n
mofle
Still doesn't work. I don't get any error message now, but it won't hide the link. My HTML: http://pastebin.com/d1963ce64
mofle
hs.match(233253) => hs.match('233253')
KooiInc
Already tried that. Doesn't work.
mofle
See: http://pastebin.com/m20ad3ce
KooiInc
Sorry, make that http://pastebin.com/d14a18cfc
KooiInc
The last pastebin you posted has expired. Can you share the code again?
mofle
http://pastebin.com/m2e80f34f
KooiInc
+4  A: 

Using jQuery:

$("a.link[href$='233253']").hide();

The $= attribute selector matches all elements where the selected attribute ends with the given value.

David Hanak
Thanks, I would really like to use jQuery, but I don't have access to it where this is going to be used.
mofle
+3  A: 
<html>
<head>
<script type="text/javascript">

function hideLinks(className, ids) {
    var links = document.getElementsByTagName("a");
    var max   = links.length;

    for (var i=0; i<max; i++) {
        var link   = new RegExp("(\s*)"+ className +"(\s*)");
        var isLink = link.test(links[i].className);

        if (isLink) {
            for (var j=0; j<ids.length; j++) {
                var regexp = new RegExp(ids[j] + "$");
                var hasId  = regexp.test(links[i].href);

                if (hasId) {
                    links[i].style.display = "none";
                }
            }
        }
    }
}

window.onload = function() {
    hideLinks("link", [233253]);
}
</script>
</head>
<body>

<a class="link" href="www.website.com?id=233253">test1</a>
<a class="link" href="www.website.com?id=456456">test2</a>


</body>
</html>

Edit: I posted a new version after reading your comment about encapsulating the functionality inside a function. This one should work just as well as the previous version.

Ionuț G. Stan
Thanks ;) Is there a way to make this code more compact? It has to be as short a possible, readability is not important.
mofle
Surely you can do this yourself: just remove whitespace and shorten variable names.
David Hanak
I can't figure out how to input the number from a variable. I'm trying to create a function from it, so I can reuse it, but I don't quite know how and there to put the variable that holds the number. /233253$/ Any thoughts?
mofle
@mofle, I updated my answer according to your last question.
Ionuț G. Stan
Awesome! Thanks ;)
mofle
+1  A: 

No offense but creating loops seems like a workaround to me. If you could add unique id's to the links that would obviously be the preferred method.

After that you could use 'getElementById' to set a different class to hide the particular link.

Roel Snetselaar
or even just set CSS rules and do away with JS altogether. #link233253 { display: none }
nickf
Unfortunately I don't have access to the html...
mofle
A: 

What distinguishes one link from another? If you know that on the server side then add appropriate classnames and which will be hidden, in a static way, from the CSS.

Dynamically determining what needs hiding will require you to dynamically generate your javascript snippet, unless you render this inside the HTML.

Update: If you don't have access to the generated HTML then my post does not help you.

cherouvim