$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How get tag attribute by javascript without using id or class? - Stack Overflow|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How get tag attribute by javascript without using id or class? - Stack Overflow

matteradmin12PV0评论

I am new in javascript.I am trying this below code to get href attribute.

<a href="facebook"  onclick="cheak()">Facebook</a>

        <script type="text/javascript">
        function cheak()
        {
            var a= document.getElementsByTagName("a").getAttribute('href');
            alert(a);
        }

</script>

I know this is working by below code

document.getElementById("myid").getAttribute('href');  // if I use any id

But in tag I don't want to use any id or class. How can I get this attribute without using any id or class in a tag.

I am new in javascript.I am trying this below code to get href attribute.

<a href="facebook."  onclick="cheak()">Facebook</a>

        <script type="text/javascript">
        function cheak()
        {
            var a= document.getElementsByTagName("a").getAttribute('href');
            alert(a);
        }

</script>

I know this is working by below code

document.getElementById("myid").getAttribute('href');  // if I use any id

But in tag I don't want to use any id or class. How can I get this attribute without using any id or class in a tag.

Share Improve this question asked Aug 21, 2015 at 6:40 Satu SultanaSatu Sultana 5371 gold badge11 silver badges23 bronze badges 2
  • Are you only trying to get the element from within the cheak function? If so, Zain's answer is the correct one. – T.J. Crowder Commented Aug 21, 2015 at 7:33
  • Side note: That href is incorrect. It should be href="http://facebook." or href="https://facebook.". Just href="facebook." is a relative URL. – T.J. Crowder Commented Aug 21, 2015 at 7:36
Add a ment  | 

3 Answers 3

Reset to default 3

Preface: Below, I've answered the question of how to find an element without using an id or class, in the general case. But if you just want the element from within your cheak function, then Zain's answer is the correct way to do that (with minimal changes to your code; arguably more correct might be modern event hookup).


But in tag I don't want to use any id or class. How can I get this attribute without using any id or class in a tag.

You reach for the Swiss army knife of element selection: document.querySelector. With it, you can use any CSS selector that matches the element:

var href = document.querySelector("any selector here").getAttribute("href");

querySelector returns the first match in the document. (There's also querySelectorAll, which returns a list.) They're both supported in all modern browsers, and also IE8.

Since you have the full power of CSS selectors, you can use anything about the position of the element in the document to find it.

There's too little context in your question for a specific example, but for instance if it's the first a in the document:

var href = document.querySelector("a").getAttribute("href");
alert(href);
<a href="facebook."  onclick="cheak()">Facebook</a>

Or using that onclick:

var href = document.querySelector("a[onclick='cheak()']").getAttribute("href");
alert(href);
<a href="facebook."  onclick="cheak()">Facebook</a>

If you pass in this when calling the function. it will pass the element reference to your function and you can use it to get value of href:

<a href="facebook."  onclick="cheak(this)">Facebook</a>
<!-- Note -----------------------------^^^^            -->

<script type="text/javascript">
function cheak(a) {
     alert(a.href); // Gives you the fully-resolved URL
     alert(a.getAttribute("href")); // Gives you the content of the `href` attribute (not fully-resolved)
}
</script>

the parameter data is the plete tag on which the function is called

<a href="www.facebook."  onclick="cheak(event)">Facebook</a>

     <script type="text/javascript">
    function cheak(e)
    {
       e.preventDefault(); 
      //  var a= document.getElementsByTagName("a").getAttribute('href');
        alert(e.target);
    }

</script>
Post a comment

comment list (0)

  1. No comments so far