Jump to content

how to get id of select menu?


shortysbest

Recommended Posts

<select class="attendance-presence" id="<?php print $row['id'];?>" onchange="send_attendance(this.value);">
<option selected>--Select--</option>
  <option value="1">Present</option>
  <option value="2">Tardy</option>
  <option value="3">Absent</option>
</select>

 

with javascript how would i get the id? like this.id. That won't work though, keeps coming back as undefined.

I have a list of these menus so it has to get the id of the one u click on

Link to comment
https://forums.phpfreaks.com/topic/214315-how-to-get-id-of-select-menu/
Share on other sites

this.id is exactly what you need. If it is coming back as undefined, then I suspect that $row['id'] doesn't have a value. have you looked at the generated HTML code to validate that the fields have values for the ID paramater?

 

I'd bet money thatthe HTML looks like this:

<select class="attendance-presence" id="" onchange="send_attendance(this.value);">

well no, the id is valid. If i change

 

onchange="send_attendance(this.value);"

 

to

 

onchange="send_attendance(this.value);current_id(this.id);"

 

and have a new function in javascript called current_id() and alert() it it comes out correctly. However the problem is i cannot get the id from one function to the other, and i cannot have a function inside of the other function.

send_attendance(this.value);

Is sending the VALUE of the SELECT tag to your function.  It is NOT sending the SELECT tag itself, so you can't get the ID (the VALUE has no ID).  Simply send "this" to the function and use this.value in the function as well as this.id

onchange="send_attendance(this);

 

I'm no Javascript expert, but that is my opinion on the matter.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.