Question

I have an html element of input type="hidden". I need to attach a datepicker to it, which gets trigerred on clicking a button.

The datepicker tries to lookup where to position itself, using the $(attachedElement).offset(), which is (0, 0) for a hidden element.

Is there a way to set the offset for a hidden input?

Était-ce utile?

La solution

I worked in a project where we had to do the exact same thing. Even though our solution become a bit more complex then the following, the example describes more or less the approach we used!

We end up wrapping the input.hidden element with a className!

<form name="shabba-form">

  <input type="text" name="fullname" value="Shabba Ranks">

  <div class="hiddenField">
    <input type="hidden" name="myHiddenInput" value="foo">
  </div>

</form>

Now, you can select the wanted input element and then check the parent location in your document!

$(document).ready(function(){

  var data = $('input[name="myHiddenInput"]').parent('.hiddenField ').offset();

  console.log(data);

});

In case you prefer, you can see a live example here http://jsbin.com/pugux/1/

  • Remember that the wrapper class can have any style, as long is not display:hidden, you can make it not visible using visibility: hidden, height: 0px, opacity: 0 and stuff like that.

Hope this helps!

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top