www.webdeveloper.com
Results 1 to 10 of 10

Thread: GetElementById Questions

  1. #1
    Join Date
    Apr 2010
    Posts
    5

    GetElementById Questions

    Hi - I'm trying to set up something up where upon submit, if the 'identifier' input field is empty, 'identifier=' won't be passed to the URL string. Any ideas? Here's the code only for this certain issue:

    Submit
    Code:
    <a href="https://X.com&" onclick="return submitForm(); pageTracker._link(this.href); return false;">Click Here</a>
    Input
    Code:
    <input type="text" id="identifier" name="identifier" />
    Javascript
    Code:
    function submitForm() {
    	if (document.getElementById) {
    var identifier = document.getElementById("identifier").value;
    
    var qs;
    qs = qs + "&identifier=" + identifier;
    
    window.open(pageTracker._getLinkerUrl("https://X.com&" + qs),this.target);
    		
    		return false;
    	}
    	return true;
    }

  2. #2
    Join Date
    Apr 2010
    Posts
    5
    any suggestions? Here's more complete code:

    Code:
    function submitForm() {
    	if (document.getElementById) {
    		var dayPicked = document.getElementById("date-1-dd").value;
    		var MonthPicked = document.getElementById("date-1-mm").value;
    		var yearPicked = document.getElementById("date-1").value;
    		var datePicked = MonthPicked + '/' + dayPicked + '/' + yearPicked;
    		var identifier = document.getElementById("identifier").value;
    		
    		var qs;
    		qs = "Rooms=1";
    		qs = qs + "&DateIn=" + datePicked;
    		qs = qs + "&Length=" + document.getElementById("select_nights").options[document.getElementById("select_nights").selectedIndex].value;
    		qs = qs + "&Adults=" + document.getElementById("select_adults").options[document.getElementById("select_adults").selectedIndex].value;
    		qs = qs + "&Children=" + document.getElementById("select_children").options[document.getElementById("select_children").selectedIndex].value;
    		qs = qs + "&RatePlanID=" + document.getElementById("select_rateplan").options[document.getElementById("select_rateplan").selectedIndex].value;
    	  qs = qs + "&identifier=" + identifier;
    		
    	  
    		window.open(pageTracker._getLinkerUrl("https://booking.XX.com/XX.jsp?hotelid=XXXX&LanguageID=1&" + qs),this.target);
    		
    		
    		return false;
    	}
    	return true;
    }
    Again I'm trying to send a form and if the certain field is blank (identifier), it's not passed to the URL.

  3. #3
    Join Date
    Apr 2010
    Posts
    5
    one more thing - this is how I kept out the identifier before if left blank (this was right in the form tag) - I want to know the syntax to put in a javascript document.

    Code:
    <form action="https://XXX.com" method="get" target="_blank" onSubmit="javascript:pageTracker._linkByPost(this);var inp = document.getElementsByName('identifier')[0];if (inp.value=='') inp.removeAttribute('name');">

  4. #4
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    Hi,

    Why not simply remove the element when the form is submitted? You could do this automagically with an event handler.


    Code:
    <script type="text/javascript">
    var submitForm = function (f){
     for(var i in f.elements){
        if(f.elements[i].value == ''){
    		f.removeChild(f.elements[i]);
        }
     }
     return true;
    };
    </script>
    Example form:

    Code:
    <form action="" method="get" onsubmit="return submitForm(this);">
     <input type="text" name="value1" value="this has value"><br />
     <input type="text" name="value2" value=""><br />
     <input type="submit" value="Submit query" />
    </form>
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

  5. #5
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by Jona View Post
    Hi,

    Why not simply remove the element when the form is submitted? You could do this automagically with an event handler.


    Code:
    <script type="text/javascript">
    var submitForm = function (f){
     for(var i in f.elements){
        if(f.elements[i].value == ''){
    		f.removeChild(f.elements[i]);
        }
     }
     return true;
    };
    </script>
    Example form:

    Code:
    <form action="" method="get" onsubmit="return submitForm(this);">
     <input type="text" name="value1" value="this has value"><br />
     <input type="text" name="value2" value=""><br />
     <input type="submit" value="Submit query" />
    </form>
    Eeer ... no. The removing of the elements from a collection should be made on using a backwards loop, because with each element who is removed the length of the collection is changing:
    Code:
    var submitForm = function (f){
    for(var i=f.elements.length-1;i>=0;--i){
        if(f.elements[i].value == ''){
    		f.removeChild(f.elements[i]);
        }
    }
    }
    Last edited by Kor; 04-22-2010 at 03:33 AM.

  6. #6
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    Ah, good catch, Kor. ;-)
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

  7. #7
    Join Date
    Apr 2010
    Posts
    5
    Thanks for the suggestions guys.

    I won't be using a form tag for this peice - the submit code is:
    Code:
    <p class="btn"><a href="https://booking.ihotelier.com/istay/istay.jsp?hotelid=6875&LanguageID=1&" onclick="return submitForm(); pageTracker._link(this.href); return false;">Check
      Availability</a></p>
    It then calls the js file containing the code where I need to somehow remove that 'identifier' attribute.

  8. #8
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    Is it as simple as not adding that segment of the query string if the field is empty?

    Code:
    function submitForm() {
    	if (document.getElementById) {
    		var dayPicked = document.getElementById("date-1-dd").value;
    		var MonthPicked = document.getElementById("date-1-mm").value;
    		var yearPicked = document.getElementById("date-1").value;
    		var datePicked = MonthPicked + '/' + dayPicked + '/' + yearPicked;
    		var identifier = document.getElementById("identifier").value;
    		
    		var qs;
    		qs = "Rooms=1";
    		qs = qs + "&DateIn=" + datePicked;
    		qs = qs + "&Length=" + document.getElementById("select_nights").options[document.getElementById("select_nights").selectedIndex].value;
    		qs = qs + "&Adults=" + document.getElementById("select_adults").options[document.getElementById("select_adults").selectedIndex].value;
    		qs = qs + "&Children=" + document.getElementById("select_children").options[document.getElementById("select_children").selectedIndex].value;
    		qs = qs + "&RatePlanID=" + document.getElementById("select_rateplan").options[document.getElementById("select_rateplan").selectedIndex].value;
    
    if(identifier != ''){ // identifier is not blank
    	  qs = qs + "&identifier=" + identifier; // add identifier to query string
    } // otherwise, leave the query string alone so that nothing is added
    	  
    		window.open(pageTracker._getLinkerUrl("https://booking.XX.com/XX.jsp?hotelid=XXXX&LanguageID=1&" + qs),this.target);
    		
    		
    		return false;
    	}
    	return true;
    }
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

  9. #9
    Join Date
    Apr 2010
    Posts
    5
    That did it - thanks very much!

  10. #10
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    Happy to help.
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

Thread Information

Users Browsing this Thread

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

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