1

I'm working with the following bit of html and am trying to select it's value in either plain javascript or jquery. The name attribute can vary so I can't use it as my selector, although it will always represent the same data (grade_level). I think my best way of selecting it is via the string 'students.grade_level, but I'm not sure how to access it.

<input type="hidden" name="UF-001008-1$formatnumeric=#########.#####" value="1" data-validation="{"maxlength":"10","isinteger":"true","type":"number","key":"students.grade_level"}">

I have so far not been able to select element's value. I have tried:

var myvar = $( "input[data-validation~='students.grade_level']" ).val();
var myvar = $( "input:contains('students.grade_level')" ).val(); 

How else can I go about this?

TYIA

1 Answer 1

3

The way that you have given your attribute is wrong,

<input type="hidden" name="UF-001008-1$formatnumeric=#########.#####" value="1" data-validation="{'maxlength':'10','isinteger':'true','type':'number','key':'students.grade_level'}">

Use quotes properly to cover your attributes, either escape the double quote or use single quotes instead.

And after correcting that you could use the *= attibute value contains selector to achieve what you want,

var myvar = $( "input[data-validation*='students.grade_level']" ).val();

:contains() selector will not work out since it would search for matching text content. Also ~= attribute contains word selector wont work out as we do not have any word in our data-validation attribute. Word in the sense, group of texts separated by a space.

DEMO

Sign up to request clarification or add additional context in comments.

2 Comments

It may be technically incorrect, but that is a straight copy/paste from view->source. It is the code generated by the product I'm working in. :(
@BrianBrock Glad to help! :)

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.