-3

I want to create a flag variable whose name is same as the div id which is clicked...(fiddle)

following is the what i tried to do... JQuery:

var $(this).attr(id)=12;
alert($(this).attr(id));

HTML:

<div id="abc">some text goes here</div>

But it didn't work please help...here is a fiddle.

in fiddle(see the value of var cccis not changing)

Bharat Soni
  • 2,824
  • 6
  • 23
  • 48
  • judging by all the wrong answer, i think your question is unclear... – A. Wolff Jun 30 '13 at 11:03
  • Is your real code missing the quotes around `id`? – Barmar Jun 30 '13 at 11:06
  • 1
    Variable variables are almost always the wrong way to do something. Either store the information in the element itself with `.data()`, or use the id as a property in an object. – Barmar Jun 30 '13 at 11:08

4 Answers4

1

The correct form would be:

$(this).attr(id, "12")

And assigning a number as ID might be problematic in non html5 context.

However, the only way to somehow use the value of the div as variable name would be in the following way (instead of window, you could create a custom scoped var x={} ):

window[ $(this).attr(id)] = 12;
// or
window[ this.id ] = 12;

Problem: you don't know the name of the value, hence you always had to address the variable in this way. There is probably a better solution for your initial problem...

If you want to store information to that specific html-element, you'd better use the data() method.

$('#ccc').on('click',function(){
    $(this).data("ccc",12);
    alert( $(this).data("ccc") );
});

The value 12 is now stored "directly on the html element". Is that what you perhaps want?

Christoph
  • 50,121
  • 21
  • 99
  • 128
  • The question is not asking how to set the attribute value. It is asking how to use the attribute value as a variable name. – Quentin Jun 30 '13 at 10:59
  • @christoph i tried your way, still value is not changing...[http://jsfiddle.net/iiison/ez4uD/4/ ] – Bharat Soni Jun 30 '13 at 11:24
  • @Bharat the correct way would be this: http://jsfiddle.net/ez4uD/5/, but that makes not much sense, better take the data method, to save the value on the html element directly: http://jsfiddle.net/ez4uD/6/ – Christoph Jun 30 '13 at 11:29
1

It's sort of clear what you're trying to do, but it's not at all clear why you're trying to do it. Variables should, well, vary... but their names should not. (This comes up a surprising number of times, actually, so I really wonder where the impetus to do this comes from.)

This is not a variable name:

$(this).attr(id)

It's a call to the $ function, which returns a jQuery object, followed by a call to the attr function on that object, which returns a value. (Or should, depending on what id is in this context. If you meant 'id' instead then it should return the string value of the id attribute on the matched element.)

This should work, in that it should alert the string value of the id attribute:

alert($(this).attr(id));

But the question is, what are you trying to do by using a function call's return value as a variable name? I know JavaScript isn't a static language, but it's not that dynamic.

The variable name has to be static, otherwise how would any other code know how to use it? There is undoubtedly a better structure to achieve the functionality you're looking to achieve.

David
  • 208,112
  • 36
  • 198
  • 279
  • actually i've already defined the variables, according to clicked div i want change the value to 0 and 1... any other ideas.. – Bharat Soni Jun 30 '13 at 11:19
  • [http://jsfiddle.net/iiison/ez4uD/4/] this is the fiddle, see here `var ccc` value s not changing... – Bharat Soni Jun 30 '13 at 11:22
  • @BharatSoni: If you've already defined the variables then why not use those variables? Again, it's very unclear what the bigger picture of the intended functionality here is, but there is definitely a structure that will meet the needs. You can store your values in arrays, objects, JSON structures, on the DOM elements themselves, etc. – David Jun 30 '13 at 11:23
  • @BharatSoni: Do you mean at http://jsfiddle.net/iiison/ez4uD/ ? There is no `var ccc` in that code. All I see there is the same mistaken structure in this question, where you try to declare a function's return value as a variable and assign a value to that variable. The language doesn't work that way. (I don't know of any that does, thankfully.) Are you just trying to change the element's `id` value? Or something else? – David Jun 30 '13 at 11:27
  • sorry my mistake I've provided wrong fiddle here is the new [http://jsfiddle.net/iiison/ez4uD/4/] – Bharat Soni Jun 30 '13 at 11:29
  • @BharatSoni: The `ccc` variable doesn't change because you never change it. You set it to `0` and then never touch it until you alert its value later. You do set another completely different value in the interim, which would be the value on `window['ccc']`. But setting a value in one object doesn't automatically update a value in another object. – David Jun 30 '13 at 11:33
  • this is the problem sir :) I wanted a solution for that.. how to do this thing without using if statement – Bharat Soni Jun 30 '13 at 11:34
  • 1
    @BharatSoni: *What* is the problem? A solution for *what*? And what does an `if` statement have to do with anything? It's completely unclear what you're trying to do at this point. When you store a value in a variable, you use that variable to refer to that value later. When you store it in a different variable, you use that different variable to refer to that value. The more I look at your code, the more it sounds like what you're really looking for is some sort of introductory tutorial to programming, because you seem to be fundamentally misunderstanding the concept of a variable. – David Jun 30 '13 at 11:37
  • let me explain everything .. I have a function named `loadCookies(name)` and 9 buttons with different values and ID's, this function loads the cookie of a user, I have 9 different cookie values, when user clicks on buttons different cookie values will be loaded to a separate box – Bharat Soni Jun 30 '13 at 11:40
  • @BharatSoni: I still don't see the problem, then. You know the name of the cookie and can access it, and you know the names of the 9 values you placed into that cookie. So you can refer to each one separately by its name. So each button would have a handler to fetch a different cookie value? They can share a generic handler if the button in some way carries a value used to fetch the intended cookie value. Perhaps you should modify the question to show more complete code and explain where you're having trouble. Because you've started down the wrong path and are only asking about that path. – David Jun 30 '13 at 11:49
  • let me explain everything.I have a function named `loadCookies(name)` and 9 buttons with different values and ID's, this function loads the cookie of a user, I have 9 different cookie values, when user clicks on any button different cookie values will be loaded to a separate box and previous data will be deleted.you can consider http://www.findyogi.com/ if you click on shortlist button,that mobile will come in box at right bottom box.now suppose there are 5 categories, then shortlist box should show respective category's shortlisted item.so that flag will represent that category name – Bharat Soni Jun 30 '13 at 11:54
  • the previous one is not complete check this one – Bharat Soni Jun 30 '13 at 11:55
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/32634/discussion-between-bharat-soni-and-david) – Bharat Soni Jun 30 '13 at 12:00
0
$('#ccc').on('click',function(){
   window[this.id] = 12;
    alert(window[this.id]);
});
A. Wolff
  • 74,033
  • 9
  • 94
  • 155
  • The question is asking how to use the id value as the *variable name*, not how to store it in a variable with a fixed name. – Quentin Jun 30 '13 at 11:01
  • you've set the value as id, i want to set the name of variable as id – Bharat Soni Jun 30 '13 at 11:01
  • maybe it is what you are looking for, or maybe not but your initial question is quite ambiguous imo – A. Wolff Jun 30 '13 at 11:07
  • that's ok @Quentin but could you provide then answer OP is looking for? I still don't get it – A. Wolff Jun 30 '13 at 11:09
  • @BharatSoni: No, he didn't set the value as `id`. He set the *indexer* in the `window` collection as the *value of* `id`, which is as close as you're going to get to a dynamic variable name. But it's going to be difficult to use because the rest of your code won't know what that indexer is without re-querying the DOM. – David Jun 30 '13 at 11:09
  • @David he edited his post... – Bharat Soni Jun 30 '13 at 11:37
  • @roasted — I could, but since it is a duplicate, I voted to close it instead. – Quentin Jun 30 '13 at 19:17
-2

You dont have to use a variable

two ways to do it

<div id="abc">some text goes here</div>

//mixing jquery and pure javascript
$('#ccc').on('click',function(){
  alert(this.id);
});

//using only Jquery
 $('#ccc').on('click',function(){
  alert($(this).attr('id'));
});
Benny Afriat
  • 393
  • 1
  • 3
  • 12