TTT
TTT

Reputation: 4434

How to escape in HTML5 data-attribute

The data-attribute stuff is new to me and just realized I need to escape space, comma, etc. Can anyone give me some suggestions on my approach? Below is my code:

#A list holds all the strings to be sent to data-attribute, 
#the sequence and length of the list is dynamic, determined by the user 
#(That's why I need to find a way to escape automatically.

Apt_p = []
Apt_p.append('Relative to planting')
Apt_p.append('Relative to harvest')
Apt_p_j=json.dumps(Apt_p) #convert everything into JSON for future usage

#I think below is the place to let escape happen.
html = """<td id="Apt_p_j" data-val=%s></td>""" %(Apt_p_j)

Upvotes: 3

Views: 4506

Answers (1)

David Grayson
David Grayson

Reputation: 87406

Put your attribute value inside double quotes so you don't need to worry about escaping spaces. Then escape it just like you would escape any other attribute in quotes. Your web framework should have a function for that.

Upvotes: 6

Related Questions