Solved

Help using .after()

Posted on 2011-09-30
2
258 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

920 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

12 Experts available now in Live!

Get 1:1 Help Now