Java Questions
Java Questions

Reputation: 7963

How to get value from Anchor

I create Anchor like the following :

<ul>
                                            <c:forEach var="categoryName" items="${categoriesList}" varStatus="category">
                                        <li><a href="#" id="${categoryName}" value="${category.index}" onclick="getProvidersList  (this)" >${categoryName}</a></li>
    </c:forEach>

I have a Anchor like the following

<a href="#" id="Landline" value="3" onclick="getProvidersList(this)">Landline</a> and I want to get the value of the above element

if you could see there is value=3 I want to get the 3

in the calling method I have

function getProvidersList(categoryIndexObjext){
            var categoryIndex = $(categoryIndexObjext).val();
            console.log("categoryIndex : "+categoryIndex);
}

but this prints nothing. so how do I get the value of an anchor ?

Upvotes: 0

Views: 416

Answers (5)

Dharmesh Thanki
Dharmesh Thanki

Reputation: 420

Try below code.

$('#Landline').attr('value');

If you will alert above code you will get answer = 3

Cheers!

Upvotes: 0

Punitha
Punitha

Reputation: 152

<a href="#" id="Landline" value="3" onclick="getProvidersList(this.value)">Landline</a>
function getProvidersList(categoryIndexObjext){
var landval=$("#Landline").attr("value");
}
then you can set value any textbox means

<input type="text" name="line" id="Lline">

Inside script 
function getProvidersList(categoryIndexObjext){
var landval=$("#Landline").attr("value");
$("#Lline").val(landval);
}

Upvotes: 1

Banana-In-Black
Banana-In-Black

Reputation: 2557

Using jQuery.attr() instead of jQuery.val() because it's a custom attribute.

Upvotes: 1

rajesh kakawat
rajesh kakawat

Reputation: 10906

try to use data attribute like this one,FIDDLE

HTML

<a href="#" id="myA" data-value="3">asdasd</a>

JAVSCRIPT

$(function(){
    alert($('#myA').data('value'));
})

Upvotes: 1

iJade
iJade

Reputation: 23811

Try changing

var categoryIndex = $(categoryIndexObjext).val();

to

var categoryIndex = $(categoryIndexObjext).attr('value');

Upvotes: 1

Related Questions