javascript - How to access a hidden Dom Element with jQuery? - Stack Overflow

I have a HTML Div (I'm using Twitter Bootstrap to hide-show div2) looking like this:HTML:<div i

I have a HTML Div (I'm using Twitter Bootstrap to hide-show div2) looking like this:

HTML:

<div id="div1">
  <button id="mybtn">Button</button>
  <div id="div2" class="modal hide fade span12" style="display: none;">
    <button id="anotherButton" data-char="">AnotherButton</button>
  </div>
</div>

With jQuery im trying to change the Value of data-char while im clicking on mybtn. But without success cause div2 is hidden. console.log($('#anotherButton')); return an empty array.

Is there a way to access hidden DOM with jQuery or Javascript?

*Edit: * By trying this dont work better , return undefined:

$('#mybtn').live('click', function(e) 
{
    //or e.preventDefault();
    $('#anotherbutton').attr("data-char", "assigned");
    alert($('#anotherbutton').attr("data-char"));
});

I have a HTML Div (I'm using Twitter Bootstrap to hide-show div2) looking like this:

HTML:

<div id="div1">
  <button id="mybtn">Button</button>
  <div id="div2" class="modal hide fade span12" style="display: none;">
    <button id="anotherButton" data-char="">AnotherButton</button>
  </div>
</div>

With jQuery im trying to change the Value of data-char while im clicking on mybtn. But without success cause div2 is hidden. console.log($('#anotherButton')); return an empty array.

Is there a way to access hidden DOM with jQuery or Javascript?

*Edit: * By trying this dont work better , return undefined:

$('#mybtn').live('click', function(e) 
{
    //or e.preventDefault();
    $('#anotherbutton').attr("data-char", "assigned");
    alert($('#anotherbutton').attr("data-char"));
});
Share Improve this question edited Aug 6, 2012 at 16:20 3logy asked Aug 6, 2012 at 16:00 3logy3logy 2,7128 gold badges49 silver badges101 bronze badges 20
  • 3 Make sure you've wrapped your code inside a DOM ready handler. – Fabrício Matté Commented Aug 6, 2012 at 16:01
  • 1 it doesn't matter if its hidden, it just matters if its on the page. If console.log($('#anotherButton')); really returns nothing than its either not on the page or there is a typo in the selector. – lbstr Commented Aug 6, 2012 at 16:01
  • Let's see more of the code. If the element is simply not visible, but still part of the DOM, this should work correctly. – Anthony Grist Commented Aug 6, 2012 at 16:02
  • 1 Wow, I just went to test it, and really it is returning undefined. Now that's interesting. – Fabrício Matté Commented Aug 6, 2012 at 16:50
  • 1 Weird though, if you copied the answers' code directly to your file and refreshed the page, it should work. – Fabrício Matté Commented Aug 6, 2012 at 16:59
 |  Show 15 more ments

4 Answers 4

Reset to default 3

You can assign it

Live demo

$(function(){
    $('#anotherButton').attr('char', "assigned");
    alert($('#anotherButton').attr('char'));
});​

on Button click

Live Demo

$(function(){
   $('#anotherButton').attr('data-char', "assigned");
   alert($('#anotherButton').attr('data-char'));
});​

It looks like data method will not work on elements which are in hidden state. You should use attr method.

This should work fine

$(function(){
  $("#mybtn").click(function(e){
    e.preventDefault();
        $("#anotherButton").attr("data-char","new value");
  });    
});

Working sample http://jsfiddle/kshyju/a7Cua/4/

The fact that it is hidden changes nothing. The problem is probably that the js code is before the button HTML. Either put it after, or put it in a listener like $(document).ready() to make sure it has been processed when your try to access it.

Try $(button:hidden)

This usually works for things like this,

Source:http://api.jquery./hidden-selector/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745442486a4627893.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信