Kern Elliott
Kern Elliott

Reputation: 1659

Jquery Mobile how to change the value of a button

Currently I have a button in which there is no value and I would like to change it when some ajax is called. However my attempts have failed can anyone help as to how I can change and retrieve the value from the button. Below is my code:

<script>
   $(document).ready(function() { 
       $('#favourite').val("4");
   });
</script>

<a href="#" id="favourite" data-role="button" data-icon="star" data-iconpos="notext" class="ui-btn-right"  value="">mark as favourite</a>         

Upvotes: 0

Views: 140

Answers (1)

dev
dev

Reputation: 4009

Try this.

$('#favourite').attr('value','4');

Here is a working fiddle.

EDIT

As it was pointed out in the comments by Tolis Emmanouilidis below the 'value' attribute for the anchor tag is invalid. So I would suggest using another data- attribute instead.

Example.

<a id="favourite" data-value="">CLICK ME</a>

and then to set the value with jQuery.

$('#favourite').on('click',function(){
   $(this).data('value','4');
});

and likewise to return the value.

$(this).data('value');

Upvotes: 1

Related Questions