Joshua Michael Calafell
Joshua Michael Calafell

Reputation: 3107

Is a Boolean automatically converted to a string when using jQuery .attr()?

Setup

So, in one area of my program, I am setting a custom html property to true:

$(document).on('click', '.profile-player-show', function(event) {
    $('.player-profile-pop').attr('inLineup', true);
}

Then, when that info is retrieved in another later function, I'm noticing that the boolean has a type of String now and it's not really a shock, I just want to know what the rules are...? Here is an example of my code in the latter:

var inLineup = $('.player-profile-pop').attr('inLineup');
console.log("In Lineup:" + inLineup + " Type of data:" + typeof inLineup);
var btnText = (inLineup === 'true') ? "Remove From Lineup" : "Add To Lineup";

Findings

The console logs:

In Lineup:true Type of data:string
  1. Only strings are allowed in html attributes

My Question:

So, is a Boolean automatically converted to a string when using the jQuery .attr() function to set a variable such as true or false?

Upvotes: 2

Views: 468

Answers (1)

Quentin
Quentin

Reputation: 943999

Yes. Attribute values can only be strings.

Upvotes: 4

Related Questions