2

I'm not sure if I'll describe this very well... but:

I have a div, and I'd like to use the id of the div as a variable in my jQuery function.

There's a variable with the same name as the ID value, and it has already been declared as a number .. at the moment I get NaN.

I just need the variable whatIsIt to land in the function as aqstn - the jsfiddle has an alert with the id.. but it doesn't translate into the number of what it is in the code.. if you know what I mean.!?

To better explain, here's a js fiddle of the problem! http://jsfiddle.net/CpFqk/

jQuery:

$('.add').click(function() {

    whatIsIt = $(this).attr('id');
    alert (whatIsIt);
    base = (base+whatIsIt);
    // base = (base+aqstn); --> This works OK
    // Some maths to get +- 15%
    fifteen = (base*.15)
    price1 = (base+fifteen)
    price2 = (base-fifteen)

    $(".price span.more").html(price1);
    $(".price span.less").html(price2);
});
colin
  • 53
  • 1
  • 7

5 Answers5

1

You can use an object instead if you want to use the id as the variable

var data = {aqstn : 236};

Then to get the value out of the data object

whatIsIt = data[$(this).attr('id')]; // or data[this.id]

FIDDLE

wirey00
  • 33,517
  • 7
  • 54
  • 65
  • Thanks for this answer, perfect! - and all the other replies. so much knowledge! I need to go and lean about data objects ^_^ – colin Apr 03 '13 at 18:38
0

You cannot share variable between HTML and Javascript in such manner. Instead, you will need to use a rel attribute to save & specify the integer value as shown here:

http://jsfiddle.net/qwFCk/1/

Francis Kim
  • 4,235
  • 4
  • 36
  • 51
  • `rel` is not a valid attribute for a div (at least not in the HTML5 spec). Using a valid custom attribute like `data-val` would be a better idea. – Ken Herbert Apr 03 '13 at 00:20
0

If you remove var from before aqstn it will be available as a property of window and you can access it via a named parameter. window[whatIsIt]

I don't recommend you do this at all, though; I'm just providing it as a last resort. wirey's answer is the more correct one.

http://jsfiddle.net/CpFqk/1/

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
0

You must put your variable "aqstn" into an object such as window (or any):

window["aqstn"] = 236;

Then use the id as a key for this object

whatIsIt = $(this).attr('id');
alert( window[whatIsIt] );

I don't know what is you purpose but there is probably a smarter way to do that :) Did you have a look on the jQuery.data() method ?

Pirhoo
  • 680
  • 8
  • 21
0

because converting this id string into a variable doesn't seem like it'll be very tidy once i add all the prices, here's another approach. it keeps the prices out of the html.

check if the id == the variable name (string) - if it does define a new variable that's used in the calculation. it means defining one for each of the prices but.. :/

if(whatIsIt=='ovrhead'){ var jammin = ovrhead };
if(whatIsIt=='aqstn'){ var jammin = aqstn };

base = (base+(jammin));
colin
  • 53
  • 1
  • 7