xenophon xenophon - 1 year ago 76
jQuery Question

Hiding last element using .last()

I have the following form:

<form id="P" enctype="multipart/form-data" action="/" method="post">
<input class="userImage" onchange="readURL(this);" type="file" name="profileImage" accept="image/x-png, image/gif, image/jpeg, image/bmp">

I have a javascript callback function where I'm trying to hide the last
<input type="file"
and add a new one:

$("#P").append("<input class=\"userImage\" onchange=\"readURL(this);\" type=\"file\""+
" name=\"profileImage\" accept=\"image/x-png, image/gif, image/jpeg, image/bmp\">");

However everytime it runs it's hiding both the
<input type="file"
elements. I have tried to put a .show() on the last element however then it shows them both.

Answer Source

Your logic is correct. Just a simple mistake of how to use last().

Use this:

$("#P .userImage").last().hide();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download