Pranav Unde
Pranav Unde

Reputation: 193

Unable to get value in jquery

I have following code.

<ul class="multi-list tabs">
<li class="width-50 selected"><a class="first" value="123456">Mark Complete</a></li>
<li class="width-50"><a class="last even" value="123456">Delete</a></li>
<input type="hidden" name="wo[sub_complete_ids][]" value="">
<input type="hidden" name="wo[sub_delete_ids][]" value="">
</ul>

I am trying to get value of li.selected a.first but its displaying undefined.

My trial code is

console.log( $("ul.multi-list li.selected a.first").val() );

What wrong in it as it not displaying the value. Expected value is 123456.

Furthermore, this ul can be multiple.

Upvotes: 1

Views: 150

Answers (1)

Metabolic
Metabolic

Reputation: 2904

Use $().attr() instead of $().val() because the later one is for input field. Preferably use jQuery's $.data(selector,key,value) method to store and retrieve data in html element. The following snippet will get the value from the first link.

$(document).ready(function() {
      alert($('ul.multi-list li.selected a.first').attr('value'));
      });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="multi-list tabs">
  <li class="width-50 selected"><a class="first" value="123456">Mark Complete</a></li>
  <li class="width-50"><a class="last even" value="123456">Delete</a></li>
  <input type="hidden" name="wo[sub_complete_ids][]" value="">
  <input type="hidden" name="wo[sub_delete_ids][]" value="">
</ul>

Upvotes: 1

Related Questions