0

Below, you can find a screenshot from the "selected"-Object, i am using. It is a select-list. I receive the object with following code (using jQuery):

var addArticle = $('.add-article'); // div.add-article
// selector is a dynamic value, that defines, which select-list should be "found"
var select = addArticle.find('[data-id=' + selector + ']'); 

The object-selection works fine. But when i want to get the data-attribute values (values are numbers), i fail to get them.

Object image here

i tried many variations to get the values, but here is an example of it

//$this.selectors is an array with all values beween data- and -id
$.each($this.selectors, function (i, el) {
    //return if last needed data attribute was read
    if (i == $this.lastAsInt) {
        return false;
    }
    // "generate" data-selector
    selector = '[data-' + el + '-id]';

    // select = object from the image (see link), also tried with data()
    location[el] = select.find(':selected').find(selector);
});

I hope, someone could understand my problem :)

1
  • It's really not clear what you're asking, but if you're trying to select the element from the data attribute you build in selector, then you need to wrap it in [] like this: selector = '[data-' + el + '-id]';. If that isn't what you need, the please edit your question to make it what you're trying to do much clearer Commented Aug 30, 2017 at 10:36

1 Answer 1

1

Lets say you have a variable where you store your "selected" object for example var myObject

Then you can simply get the data attribute with jQuery's .data() :

myObject.data('room-id');

Here is a Fiddle

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

2 Comments

in my case this does not work. And i have to access the data attribute via myObject.data(variable).
I am so sorry. My problem was, that the object could not be read inside the function (bc accessibility...)

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.