View Full Version : Help My BBCode Bulleted List Button
Joseph Witchard 10-15-2009, 06:49 AM function goBullet()
{
var formBody = document.getElementById("body");
var listCount = prompt("Please enter the number of items in the list.", "1");
if (listCount == null || listCount == "")
{
formBody.value += "";
}
else
{
var listCountInt = parseInt(listCount);
var listItem = new Array();
formBody.value += "";
for (var i = 0; i < listCountInt; i++);
{
listItem[i] = prompt("Please enter an item.", "");
if (listItem[i] != null || listItem[i] != "")
{
formBody.value += "" + listItem[i] + "";
}
}
formBody.value += "";
}
}
The problem I'm having is no matter what number I'm entering, the for loop only executes once, thus only adding the first list item. Any ideas?
iGeek 10-15-2009, 08:05 AM First, I'd use jQuery.
I wouldn't bother asking for the number of items - most of the time I don't know how many items I'll add, and restriction is never a good thing. For this reason, the for loop is not needed. In fact, the entire mindset of the script is based on the fact that you ask for the number of list items. I'd do something like this:
$(document).ready(function(){
// List button click
$('input#bbcode_list').click(function(){
var body = $('#body').val();
var list = '';
var item = '';
var prev_item = true;
while( prev_item == true )
{
item = prompt('Enter a list list item or leave blank to exit.','');
if( item == '' || item == null )
{
prev_item = false;
}
else
{
list += ''+item+'';
}
}
$('#body').val(body+list);
});
});
Works perfectly: http://dev.phreakyourgeek.com/list.html
Joseph Witchard 10-15-2009, 01:00 PM To be honest, I have no idea what jQuery does. I'm not even sure if my host supports it.
iGeek 10-15-2009, 04:05 PM jQuery is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development. jQuery is designed to change the way that you write JavaScript.
It is a JavaScript library, which means it is client-side and has no interaction with your server. I've been using it for a while now and I have to say it is so much easier and faster to use than straight JavaScript or other JavaScript libraries. All you really need to do is add this to your header:
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>
Which will get the latest version from Google's code repository. Obviously you'll want to check out the documentation (see jQuery.com (http://www.jquery.com)), and there are some great tutorials online.
Joseph Witchard 10-15-2009, 09:36 PM Thanks, I'll look into it:)
Exactly how much faster could it be, though? I'm only using JavaScript to insert the BBCode into the textarea, and it seems to work pretty quickly for that. Changing each one to its respected HTML, I'm using PHP Regular Expressions.
iGeek 10-17-2009, 09:13 AM Faster as in less code, not necessarily less time. For example, to load a page using AJAX:
$('#content').load('./newpage.php');
You can even pass POST parameters:
$('#content').load('./content.php',{page: 'home'});
No initialization of an AJAX object, cross-browser support, etc. And XML parsing is SO much easier.
+1 for jQuery. It makes it very easy to develop dynamic web applications. I'd even say it is faster, because it's only ~9kB gzipped and minified and all the extra code you'd otherwise use is gone...
Joseph Witchard 10-27-2009, 09:45 PM Can you use external files or does each jQuery script have to be embedded in the main HTML file?
Just use the HTML iGeek provided. At the top of every page, in between the <head> tags.
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>
That file is hosted on Google's servers, but you might want to put it on your own server.
iGeek 11-01-2009, 06:29 AM Can you use external files or does each jQuery script have to be embedded in the main HTML file?
They can be anywhere JavaScript would/should be. :) External, internal, wherever - as long as you have the main file included (I posted it earlier and Tom quoted it) somewhere everything will work.
|