0

I have set a var in my code then made it global, but for some reason, dialog is showing it as 'undefined'. Can you not have var in title? Thanks

In .click event

box = $('#EB_custref').val();

Outside of function

var box;

In dialog options

title: 'Edit box' + ' ' + box,
3
  • 2
    you need to post more code... Commented Jul 28, 2011 at 11:31
  • @william. why have you downvoted me? Commented Jul 28, 2011 at 11:37
  • how do you know it was me? I did not downvote you. Commented Jul 28, 2011 at 13:01

2 Answers 2

2

The title: 'Edit box' + ' ' + box line gets run when you instantiate your dialog: I assume you're doing that on $(document).ready. At that point, your box variable is undefined.

When you set box on the click event it's too late - the title has already been set.

See this post for further info.


EDIT

Here's a demo of one solution for this:

HTML

<button data-title="Apple">OPEN 1</button>
<button data-title="Banana">OPEN 2</button>

<div id="MyDialog">
    Example Dialog Content
</div>

JQUERY

var globalTitle = ''; // Your global variable

// Startup operations
$(function () {

    $('#MyDialog').hide();
    $('button').click(function () { 
                          openMyDialog($(this).data('title')) 
                      });
});

// Open the dialog using the global myTitle variable
function openMyDialog(customTitle)
{
    globalTitle = customTitle;
    $('#MyDialog').dialog({title : globalTitle});
}

Note the use of HTML5-style data- attributes, which rock, and are accessible in jQuery through the .data() function. Also note that I've used a global variable to show you that it's possible to use one. However there's no need for it - the best approach would be to pass customTitle straight into the dialog() call, i.e. $('#MyDialog').dialog({title : customTitle});

Sign up to request clarification or add additional context in comments.

8 Comments

Thank you for that james. I was aware of hacking the css directly.
How do i then james based on your comment, make the box global from the click event? thanks
The box var is global, it's just that you used it to set the title property when it was undefined. Because title is just a string, it doesn't matter if you subsequently change the value of box. title will remain the same literal string i.e. 'Edit box undefined'. The best thing for you to do is run the dialog instantiation code on the click event.
Can you give me an example of that please james. Quite new to js & jquery. Thanks
James. Thank you. However, I am still getting errors. Here's the scenario. I need to capture this value which I do to populate the form: var custref = $("tr.trSelected td:nth-child(8) div").text(); $("#boxeditform").find("#EB_custref").attr({value: custref }); It is value I am trying to capture to make var for title. thanks
|
0
alert($('#EB_custref').val())

Check that its returning value or not

and if possible post more code/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.