How can one get the form input value of element that has a css id with special characters?


#1

Greetings fellow meteorites!
In my entire life (when writing css id’s and classes that have multiple words) I have always used dashes (i.e. - ) as separation. For example position-description below

<input id="position-description" type="text" placeholder="back of the line" /> 

And who cares ???
Well when I target the form submit event in the client javascript, I can’t really access the value in the input by calling event.target.position-description because the - will cause an error in meteor.

The obvious workaround is to just camel-case the css id (i.e. positionDescription)
But before I abandon the css notation I have been using for the past 5 years, I was wondering if there was a workaround specifically using the event.target way (as opposed to jquery) ??

Thank you and look forward to your response!
Alex


#2
event.target['position-description']

would be the usual answer, but the id value is not a property of the event object (although id is).

If you want the value of the input element from the event object you would use

event.target.value

#3

Hm right. If event.target is the form and “positionDescription” is the camelcased id, you could make the call event.target. positionDescription.value to access the value. … But is there a way to get it keep the css id notation of “position-description” ?


#4

console.log(event.target) and you can check if response object have something related ?

if not maybe use another jquery match instance.$(’#position-description’)


#5

No. As I said, the actual id value (position-description in this case, but it doesn’t matter what it is) is not a property of the event method, so you cannot access it as you are describing.

In this case, event.target.id will be set to 'position-description', and event.target.value will contain the value of that DOM element.

As @shock says, if you are in any doubt, console.log the event object in your event handler.


#6

Thank you for your help! Appreciate it.