Solved

Help using .after()

Posted on 2011-09-30
2
256 Views
Last Modified: 2012-06-27
I have the following function that, when a option item is changed, it replaces it with a 'please wait' prompt:

function showLoadingSideText(ctrl) {
	$(ctrl).hide().after('<span style="font-weight:bold; white-space:nowrap; color:#999999"><img src="/images/waiticon.gif" alt="" /> Loading, please wait...</span>');
}

Open in new window


It is invoked like this:

<select id="ddl_site" onchange="showLoadingSideText(this);" />

Open in new window


What I need to do if possible is to expand this code to set the width of the replacing span to be the same as the element that is being hidden.  This would prevent any movement of content around the control being hidden (e.g. table cells suddenly resizing because the content was much smaller).  I've managed to read the size of the element to hide...

function showLoadingSideText(ctrl) {
 var controlWidth = $(ctrl).width();
 window.alert(controlWidth);
	$(ctrl).hide().after('<span style="font-weight:bold; white-space:nowrap; color:#999999"><img src="/images/waiticon.gif" alt="" /> Loading, please wait...</span>');
}

Open in new window


...but I have some questions on the next step:

1. How do I apply the value of controlWidth into the span I am adding using after() ?
2. Why does the window.alert() message fail to show when using IE7 or IE8 (works fine in IE6,9,10 and FF)?  This might be an IETester bug though - but would like some feedback please!

Thanks.
0
Comment
Question by:Rouchie
2 Comments
 
LVL 9

Expert Comment

by:richard_hughes
ID: 36890888
Hello Rouchie

1)

The easist way to control with width of elements is by using CSS. Try adding a common class to both the <select> and the <span> as in the attached code sample.

2)

IETester is not very good with JavaScript. It is mainly used for display testing. A good way to test JavaScript in other browsers is to try them directly. Have a look here for some virtual machines which come pre-installed with the respective browser versions (IE 6, 7, 8, 9): http://www.microsoft.com/download/en/details.aspx?id=11575

Thanks,

Richard Hughes
.commonWidth
{
    width: 150px;
}

Open in new window

0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 36891002
function showLoadingSideText(ctrl) {
 var controlWidth = $(ctrl).width();
 window.alert(controlWidth);
      $(ctrl).hide().after('<span style="font-weight:bold; white-space:nowrap; color:#999999;width:' + controlWidth +'px;"><img src="/images/waiticon.gif" alt="" /> Loading, please wait...</span>');
}
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now