Mark
Mark

Reputation: 53

Not making it a string

In this situation, for example, if $(this).text() becomes 0, it will not work since in the switch 0 is not equal to "0".

In my assignment, I cannot remove " " from case.

So basically $(this).text() has to become a string somehow. Any ideas?

Thanks!

...
$( '.xyz' ).click( function()...
  var key = $(this).text();
  switch( key ) {
    case "0":
    case "00":
    case "1":
    case "2":
    case "3":
    case "4":
    case "5":
    case "6":
    case "7":
    case "8":
    case "9":
    ...

https://gist.github.com/188d81480a1503d1985b

Upvotes: 3

Views: 80

Answers (3)

Nowshath
Nowshath

Reputation: 842

Try this

var key = String($(this).text());

Upvotes: 1

earlonrails
earlonrails

Reputation: 5182

You can do this

var key = "" + $(this).text();

Which will always be a string. IE

var b = "" + 0 ;
// b = "0"

Upvotes: 2

Raekye
Raekye

Reputation: 5131

If I understand you want the numeric value?

var key = parseInt($(this).text()); //or
key = parseDouble($(this).text());

Edit 2: Thanks for the suggestion Derek. Always forget that:

+$(this).text(); //also converts to integer; parseint can be given a radix (base). + if string is already in the form of a base 10 integer

Edit: Also, your code looks wrong; $(this).text()) has an unbalanced paranthesis. text() always returns a string. Which is why I made the assumption above. Although your question seems to imply you want to search by the result of text()? In that case you would want a string though, which text() gives you.

If you could give us more details and code it'd be easier to understand

Upvotes: 3

Related Questions