1

I have the following Html code:

<tr>
   <td><a href="#" class="block" serverid="@storedServerId"></a></td>
<tr/>

But how can I get the 'serverid' attribute's value to use in my jQuery script?

This is what I tried:

var srv= document.getElementById('storedServerId').value;

But it returns simple text not the vlaue!

Thanks,

0

4 Answers 4

2

To get attributes in Jquery,use

$("your-id").attr("your-attr");

WHILE for your HTML 5 data-* attributes, you could use,

$("your-id").data("your-attr");

In your case,it would be.

var j=$(".block").attr("serverid");
alert(j);
Sign up to request clarification or add additional context in comments.

2 Comments

Now I have another question. In case I have multiple cells and in each one of them server ID is unique. How to retreive all values? Any idea!? Best, RadKM
I found out how to do it!
1

To use data attribute you have to add data-serverid in your anchor And you can get it in jquery by data() function.

  <tr>
       <td><a href="#" class="block" data-serverid="@storedServerId"></a></td>
<tr/>

$(document).ready(function(){
 alert($('a').data('serverid'));
})

Comments

0

This is how to get attributes from multiple classes:

function getServerID() {

        var sid = $('.block').each(function () {

           alert($(this).attr('serverid'));
        });

Comments

-1

HTML

<tr>
<td><a href="#" class="block" data-serverid="@storedServerId"></a></td>
<tr/>

jQuery

$('.block').data('serverid')

gets you value of your data attribute

http://jsfiddle.net/2rowezve/

3 Comments

,you have added the data- attribute yourself,OP asked for directly getting the attribute.
Of course I did. That's my attempt on fixing his problem. I see you have problem with that.
When you shoot an arrow and miss the target, you adjust your aim, not move the target to where the arrow passed. Your answer is worth noting for him, but he may not control the HTML, or may not be able to change it for some reason.

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.