www.webdeveloper.com
Results 1 to 4 of 4

Thread: Basic Listbox Adding

  1. #1
    Join Date
    Jan 2009
    Posts
    40

    Arrow Basic Listbox Adding

    Hey all,

    Got an easy one, well, what I would normally been ok with but must've been working too hard - I can't see why I'm having this problem.

    Just trying to add items to a multiple select listbox:

    Code:
    <SELECT Name="servTimeRange" multiple="multiple">
    	<?php showTimeRange(); ?>
    </SELECT>
    Code:
    <a href="#" onClick="addTimeRange(document.getElementById('servTimeFrom').value,document.getElementById('servTimeTo').value); return false;">Add</a>
    Code:
    function addTimeRange(from,to) {
    	var tstampFrom;
    	var tstampTo;
    	arrFrom = from.split(":");
    	arrTo = to.split(":");
    	if (document.serviceAdd.servTimeFromM.options[document.serviceAdd.servTimeFromM.selectedIndex].text == "pm") { (arrFrom[0] += 12); }
    	if (document.serviceAdd.servTimeToM.options[document.serviceAdd.servTimeToM.selectedIndex].text == "pm") { (arrTo[0] += 12); }
    	tstampFrom = arrFrom[0]+':'+arrFrom[1]+':00';
    	tstampTo = arrTo[0]+':'+arrTo[1]+':00';
    	var opt = document.createElement("option");
    	opt.text = from+document.serviceAdd.servTimeFromM.options[document.serviceAdd.servTimeFromM.selectedIndex].text+' - '+to+document.serviceAdd.servTimeToM.options[document.serviceAdd.servTimeToM.selectedIndex].text;
    	opt.value= tstampFrom+' - '+tstampTo;
    	document.getElementByID("servTimeRange").options.add(opt);
    }
    Problem is just on the last line of the last group of code here, giving me the "object does not support this property or method" message - and it looks right to me, so probably needs a fresh pair of eyes.

    I've also noticed that on the line "arrFrom[0] += 12", that despite the value in this array is a number, it's concatenating it - what's the best way to convert that value to an integer?

    Thanks all,
    Jess.

  2. #2
    Join Date
    Jan 2009
    Posts
    40
    Hi again,

    Still not having any luck, but narrowing it down a little... Someone able to point out my fault?

    Code:
    function addTimeRange(from,to) {
    	var vlist = document.getElementByID("servTimeRange");
    	
    	var tstampFrom;
    	var tstampTo;
    	
    	arrFrom = from.split(":");
    	arrTo = to.split(":");
    	
    	if (document.serviceAdd.servTimeFromM.options[document.serviceAdd.servTimeFromM.selectedIndex].text == "pm") { (arrFrom[0] += 12); }
    	if (document.serviceAdd.servTimeToM.options[document.serviceAdd.servTimeToM.selectedIndex].text == "pm") { (arrTo[0] += 12); }
    	
    	tstampFrom = arrFrom[0]+':'+arrFrom[1]+':00';
    	tstampTo = arrTo[0]+':'+arrTo[1]+':00';
    	
    	optText = from+document.serviceAdd.servTimeFromM.options[document.serviceAdd.servTimeFromM.selectedIndex].text+' - '+to+document.serviceAdd.servTimeToM.options[document.serviceAdd.servTimeToM.selectedIndex].text;
    	optValue = tstampFrom+' - '+tstampTo;
    	
    	var opt = document.createElement("OPTION");
    	opt.text = optText
    	opt.value= optValue
    	vlist.options.add(opt);
    		
    	//var optRange = new Option(optText, optValue);
    	//document.getElementByID("servTimeRange").options.add(opt);
    }
    The same error is now at the first line of this function, so it's very weird... Or is it, can someone say?

    Thanks,
    Jess.

  3. #3
    Join Date
    Jun 2008
    Posts
    62
    The Method is called "getElementById" not "getElementByID". Besides, you seem to have set only a name-Attribute in yout HTML, not an id-Attribute.

  4. #4
    Join Date
    Jan 2009
    Posts
    40
    Malgrim, Thank you!! The case-sensitivity was all it was.

    Yeah, I've been confused by the method before as I've been able to use it on objects without an ID attribute... The name has always sufficed for me before.

    Cheers!
    Jess.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles