2

I'm trying to figure out why my function $ is not displaying the location of my link:

<html>
    <head>
        <title>Link Test</title>
    </head>
    <body>
        <a id="mylink" href="hxxp://mysite.com">Click me</a><br>
        <script>

            $('mylink').href

            function $(id)
            {
                return document.getElementById(id)
            }
        </script>
    </body>
</html>

3 Answers 3

1

here is your code modified to output the href value in three different ways, pick which ever you like.

<html>
    <head>
        <title>Link Test</title>
    </head>
    <body>
        <a id="mylink" href="hxxp://mysite.com">Click me</a><br>
        <span id="out"></span>
        <script>

            console.log($('mylink').href);
            document.getElementById('out').innerHTML=$('mylink').href;
            alert($('mylink').href);
            function $(id)
            {
                return document.getElementById(id)
            }
        </script>
    </body>
</html>

however I am pretty sure that's not your intent. I am not sure why you are using $ as your function name, nor why are you trying to output the href. Your intent is not clear, but I have a feeling you are not approaching things correctly.

Sign up to request clarification or add additional context in comments.

Comments

1

You aren't doing anything with the href property.

Your code is equivalent to:

<script>
"hxxp://mysite.com";
</script>

You need to pass it to a function that will display it (such as console.log or alert).

2 Comments

For example, document.write("Link Address: " + $('mylink').href) OR document.write("Link Address: " + document.getElementById(id)) ?
If you want to write the value of the href property then you need to mention it. It doesn't matter if you use getElementById directly or from your wrapper function.
1

I personally don't see anything wrong with your code... Though it may not be perfect and fit all the standards, it seems fine to me. If you just want to output it or something, because it isn't doing anything now, use the following code:

document.write("Link Address: " + $('mylink').href)

Here is an example.

3 Comments

I really wish members of SO would not down vote comments without at least explaining why it needs to be downvoted. This is why I don't like coming to SO anymore.
And I can't even upvote @king Shimkus because I don't have enough points. This is not right
I don't care about the rep. I care that you found a solution. Did I solve your problem?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.