0

I'm not overly confident in my own abilities but here goes. I want JQuery to find the data attribute of all elements with a particular class. Then I want it to add them to the ajax dataString array.

There are 6 elements with that class all of which have the data attribute. Rather then include them all by id attributes surely their is an easier way. Would this work.

Here goes

var data[] = $('.sidescroller').attr('data');

var dataString = 'function=' + scripts + '&data[]=' + data[];

//execute PHP
$.ajax({
        type: 'POST',
        data: dataString,
         url: '<?php echo $thisposturl;?>?scripts',

Any ideas how to do it.

Marvellous

1
  • What are trying to achieve here? do you want to set the data attribute values of all element with class sidescroller? If so how do you want the data, should they be coma separated? Commented Apr 27, 2011 at 11:26

5 Answers 5

3

Firstly, No, .attr() will only get the attributes of the first element in the list

var data = $('.sidescroller').map(function() {
    return $(this).attr('data');
}).get();

Secondly, don't construct query parameters by hand, it's error prone and can also be abused:

Send the data parameter as a set of key-value pairs thus:

data: {
    'function': scripts,
    'data': data
}
Sign up to request clarification or add additional context in comments.

1 Comment

+1 Good solution! However, I do wish to point out the incorrect usage of the data attribute. Please see my revised answer for further info on this topic.
0

I think the .attr() function works on the first element among the matched elements a better way would be to use the map function.

var dataArr = [];
$(".sidescroller").map(function(){
    dataArr.push($(this).attr("data"));
});

Comments

0
var dataString = 'function=' + scripts;

$('.sidescroller').each(function() {
    dataString += '&data=' + $(this).attr('data');
}       

//execute PHP
$.ajax({
 type: 'POST',
 data: dataString,
 url: '<?php echo $thisposturl;?>?scripts',

Would this work?

4 Comments

@Alnitak's is probably the better option.
and if 'data' should happen to contain characters that should have been escaped?
@Alnitak are you looking for some sort of prize? I already acknowledged that yours was the better option.
Nah, I've reached my rep cap already - I was just pointing out exactly why you shouldn't build query strings by hand.
0

I think what you are looking for is

var data= [];
$('.sidescroller[data]').each(function(){
    data.push($(this).attr('data'))
});
var strdata = data.join(",");
alert(strdata );

You can find a working sample here.

Comments

0

According to HTML5 specs. custom data attributes should be named like this: data-name. Then you can use .data to fetch the data. Example below:

HTML

<div id="foo" class="has-data" data-test="foo"></div>
<div id="bar" class="has-data" data-test="bar"></div>

JavaScript

var data = {};
$(".has-data").each(function(){
    data[this.id] = $(this).data("test");
});

$.post('/', data);

2 Comments

two problems - 1. .map() returns a jQuery object, not an array - 2. data should be a key/value pair object.
@Alnitak, oh yes you are right about this, thanks for pointing it out. Thought about it the wrong way, so I will revise my answer.

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.