2

I have a button like so:

<button id='abc' value=['fred', 26]></button>

I want to know how I can access those two values via JQuery's .val() function.

I have tried $("#abc").val()[0] but that just appears to give me the first character which is the bracket "[".

I would like .val()[0] to instead give me the value "Fred".

Matt
75.5k26 gold badges156 silver badges181 bronze badges
asked Jan 5, 2012 at 11:44
1
  • You really want a button that displays "['fred', 26]" to the user? Perhaps you want to use data-values='["fred", 26]' (note that I've also fixed the invalid HTML). Commented Jan 5, 2012 at 11:49

4 Answers 4

9

Your HTML is invalid. You should try;

<button id='abc' value="['fred', 26]"></button>

However, you should look at using valid JSON notation, to enable you to parse the data as an array. JSON requires double quotes for strings (rather than single '), so you should end up with;

<button id='abc' value='["fred", 26]'></button>

If you were to access $('#abc').val() directly now, you'd retrieve the string ["fred", 26]. To retrieve this data as an array, you should parse the data using jQuery.parseJSON();

var array = jQuery.parseJSON($('#abs').val());
for (var i=0;i<array.length;i++) {
 alert(array[i]);
};
answered Jan 5, 2012 at 11:46
Sign up to request clarification or add additional context in comments.

1 Comment

#Matt: Fair enough. :-) We all have our things.
2

Matt's suggestion is good (and he's right that your HTML is invalid).

Another approach, since the element has an ID, is to store the values separately, in your JavaScript rather than in the markup. Since IDs are unique on the page by definition, you could have:

<script>
var elementValues = {};
elementValues.abc = ['fred', 26];
</script>

...and then where you want to use it, if you know the ID (in your code you do, you're using it to find the element via $("#abc")):

var values = elementValues.abc;

...or in an event handler where you don't necessarily know the ID in the code, assuming this is the element in question (as it is in event handlers in jQuery):

var values = elementValues[this.id];
answered Jan 5, 2012 at 11:51

Comments

2

You can use eval

<button id="abc" value='["fred",26]'></button>
var values = eval($("#abc").val());
values[0]
values[1]

It will return array of values

answered Jan 5, 2012 at 11:58

Comments

0

Fix correct JSON format using " insted of ':

<button id="abc" value='["fred",26]'></button>

Parse the string:

$.parseJSON( $('#abc').val() )[0];

or

var abc = $.parseJSON( $('#abc').val() );
abc[0];
answered Jan 5, 2012 at 11:58

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.