[RESOLVED]Parsing content with JQuery question

How would I write JQuery to do this:

<label for="MSG303

Find this label with just this information, put it into a var, and do an if then to so I can show a pop up?

   $(document).ready(function ( ) {  
            var msg = "Find the 303 input element above"
if(msg){ $("#dialog").dialog();
} }

 

jQuery supports an
attribute-equals selector
that you can use as follows :

$("[for='MSG303']").html("Example!")

A complete example might look like the following :

<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<script type='text/javascript'>
  $(function(){
    // Find the label with the for attribute of "MSG303" and set it
    $("[for='MSG303']").html("Example!")
  });
</script>

So your existing code would just need to use :

$(document).ready(function ( ) {  
      // Attempt to find your "MSG303" element
      var msg = $("[for='MSG303']");

      // Check the content of the message (and ensure you were able to find your element)
      if(msg != undefined && msg.html().length > 0){
             // Display your dialog
             $("#dialog").dialog();
      }        
}

I created another example that might more closely resemble your requirements :

<!DOCTYPE html>
<html>
<head>
<!-- jQuery & jQuery UI References -->
<link href="//code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.min.css" rel="stylesheet" type="text/css" />
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="//code.jquery.com/ui/1.11.1/jquery-ui.min.js"></script>
<script type='text/javascript'>
  $(function(){
    // Attempt to find your "MSG303" element
    var $msg = $("[for='MSG303']");

    // Check the content of the message (you can use .val() or .html())
    if($msg != undefined && $msg.html().length > 0){
         // If it exists, load the content of your dialog with your message
         $("#dialog").html($msg.html()).dialog();
    }      
  });
</script>
</head>
<body>
  <!-- Example Label -->
  <label for="MSG303" style='display:none'>This is an example of your content</label>
  <!-- Dialog placeholder -->
  <div id='dialog'></div>
</body>
</html>

Thanks Rion, that’s excellent!

Leave a Reply