1

I need to get value from paragraph tag

<div id="dialog-confirm" title="<fmt:message key="label.modal.confirmation" />">
</div>
<div style="display:none" id="parent">
<p class="yes"><fmt:message key="button.modal_ok" /></p>
<p id="no"><fmt:message key="button.modal_no" /></p>
</div>

in my javascript function

$(function() {
var butok = $('#parent').children('p.yes').text();
$( "#dialog-confirm" ).dialog({
  autoOpen: false,
  resizable: false,
  height:140,
  modal: true,
  buttons: {
      butok : function() {
      window.location.href = partsArray[0];
      $(this).dialog( "close" );
    },
    butno: function() {
        location.reload();
      $( this ).dialog( "close" );
    }
  }
});
});

How can i get it? .text() and .html() does not work

Ilya
  • 41
  • 8

1 Answers1

2

You can't directly use butok as you did, because the button text will be "butok", not the value of the variable. To be able to do that, you need to add the property separately. More info here.

Fixed code

var butok = $('#parent').children('p.yes').text(),
    butno = $('#parent').children('p#no').text();

// Define an empty Object
var myButtonsObject = {};

// Add properties to it using the [ bracket ] notation
myButtonsObject[ butok ] = function() {
    window.location.href = partsArray[0];
    $(this).dialog("close");
};

myButtonsObject[ butno ] = function() {
    location.reload();
    $(this).dialog("close");
};


$("#dialog-confirm").dialog({
    autoOpen: false,
    resizable: false,
    height: 140,
    modal: true,
    buttons: myButtonsObject  // Use it
});

Demo

$(function() {

  var butok = $('#parent').children('p.yes').text(),
    butno = $('#parent').children('p#no').text();

  // Define an empty Object
  var myButtonsObject = {};

  // Add properties to it using the [ bracket ] notation
  myButtonsObject[butok] = function() {
    window.location.href = partsArray[0];
    $(this).dialog("close");
  };

  myButtonsObject[butno] = function() {
    location.reload();
    $(this).dialog("close");
  };


  $("#dialog-confirm").dialog({
    autoOpen: true,
    resizable: false,
    height: 140,
    modal: true,
    buttons: myButtonsObject // Use it
  });

});
<link href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>

<div id="dialog-confirm" title="Modal title"></div>
<div style="display:none" id="parent">
  <p class="yes">Yeeeees</p>
  <p id="no">Nooooo</p>
</div>
Community
  • 1
  • 1
blex
  • 24,941
  • 5
  • 39
  • 72
  • Dont know what i am doing wrong, but it still is not working – Ilya Aug 24 '15 at 22:42
  • @Ilya Open your JavaScript console (F12, and select the `console` tab). Do you see any errors? – blex Aug 24 '15 at 22:43
  • yes. Uncaught Error: cannot call methods on dialog prior to initialization; attempted to call method 'open' – Ilya Aug 24 '15 at 22:49
  • @Ilya Haha, you found what was wrong? Good job, and you're welcome! I added a demo under my answer, it should work. – blex Aug 24 '15 at 22:54
  • Yes, i just lost the bracket. I saw, you really help me, thank you one more time! – Ilya Aug 24 '15 at 22:58