Imagine the reaction to Palestinians blowing up a residential Israeli apartment building with hundreds of civilians inside and justifying it by saying they wanted to kill an IDF member inside.
If the IDF member is shooting at them from the apartment building, then it becomes a valid military target. This is very clearly spelled out in the Geneva conventions.