0

I'm trying to set a variable attribute name, ie <a data-33="string">, with 33 being a random number generated by Math.random().

I have tried:

var randomNum1 = Math.floor(Math.random() * (200 - 100 + 1)) + 100;
var attrb = 'data-' + randomNum1.toString(); 
$(this).attr({
    attrb : 'someString',
    attrb2 : 'someString'
});

But instead of adding an attribute of data-randomNumber="someString", it adds attrb="someString"

I'm trying to pass multiple attributes. I know I'm missing a basic javascript concept, but any help would be appreciated!

5
  • 1
    you can't use an object literal to do that... Commented Jan 30, 2014 at 4:36
  • possible duplicate of Using a variable for a Javascript object key Commented Jan 30, 2014 at 4:40
  • @Bergi Not exactly a duplicate since the user is unaware of proper usage and his understanding is will be different. Commented Jan 30, 2014 at 4:44
  • @user3251562 IMHO do try to understand javascript objects and the way they can be accessed. Only knowledge of JQuery wont help you write good code. Commented Jan 30, 2014 at 4:46
  • +1 @LearningNeverStops Its crucial that people understand pure JavaScript with its objects and other concepts. JQuery is spoiling people :) Commented Jan 30, 2014 at 4:50

6 Answers 6

2

When you are doing, {attrb: "something", attr2: "something"}, the keys, attrb and attr2 are not evaluated and are taken as it is. So you cannot use variables over there.

To solve your problem, what you can do is, call the attr() method with two arguments - 1: the attribute, 2: the value:

var el = $(this);
el.attr(attrb, 'someString');
el.attr(attrb2, 'someString');

In this case the first argument of attr() is evaluated and the variable is substituted with the value.

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

2 Comments

Thanks, admittedly I am spoiled with JQ, but am learning JS quickly with help from people like you!
Welcome! :) This will help A LOT. developer.mozilla.org/en-US/docs/Web/JavaScript/…
1

try this

var randomNum1 = Math.floor(Math.random() * (200 - 100 + 1)) + 100;
var attrb = 'data-' + randomNum1.toString();
$(this).attr(attrb,"someString");
$(this).attr(attrb2,"someString");

Comments

0
var randomNum1 = Math.floor(Math.random() * (200 - 100 + 1)) + 100;
var atts = {};
   atts['data-' + randomNum1.toString()] = 'someString';
$(this).attr(atts);

Setting the names of object literals with variables works using square brackets.

Comments

0

jQuery has a built in data() function see here for the proper method documentation. As for your specific propblem I would suggest

var randomNum1 = Math.floor(Math.random() * (200 - 100 + 1)) + 100;
var attrb = randomNum1.toString(); 
var $this = $(this);
$this.data(attrb, 'someString');
$this.data(attrb2, 'somestring');

1 Comment

Object literals are taken as it is, attrb wont be replaced with its value.
0

Try this:

function getRandom(){
    return (Math.floor(Math.random() * (200 - 100 + 1)) + 100).toString();
}

var attrs = {};

attrs["data-" + getRandom()] = "something";
attrs["data-" + getRandom()] = "something";
attrs["data-" + getRandom()] = "something";

//console.log(attrs);

$(".target").attr(attrs);

This will do something like:

<div class="target" data-108="something" data-123="something" data-107="something">something</div>

What I am doing is first creating the attributes obj separately, then passing in the .attr() method of jQuery.

Working Demo: http://jsfiddle.net/xBdcq/

Comments

0

try like this

$(this).attr('attr-'+b,'somestring');

Comments

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.