-1

I am using Bootstrap and I have two identical forms. I am trying to add form submission to Google Search results and it works but when I include two of the same form it doesn't work because of the id being the same on both. How can I fix this? The ID needs to be the same because google looks for the "G". The reason I have two forms is because I have it displayed differently on mobile. Using media queries. Below is my code thanks.

<form name="globalSearch" class="navbar-form" role="search" form action="" onsubmit="return validateSearch()">
        <div class="input-group add-on">
          <input type="hidden" name="cx" value="" />
          <input type="hidden" name="cof" value="FORID:11;NB:1" />
          <input type="hidden" name="ie" value="UTF-8" />
          <input class="form-control" placeholder="Search entire site..." id="q" name="q" type="text">
          <div class="input-group-btn">
            <button class="btn btn-default btnSubmit" type="submit">
              <i class="glyphicon glyphicon-search"></i>
            </button>
          </div>
        </div>
</form>

function validateSearch() {
        if (globalSearch.q.value.length == 0) {
            document.getElementById("q").value = "Enter a Value";
            document.getElementById("q").style.color = "red";
            return false;
        }
    }
5
  • It is bad practice to have repeated ids on a single page. Use different ids and keep the same name. Commented Feb 21, 2017 at 14:06
  • that "q" is necessary for google. i cant change it Commented Feb 21, 2017 at 14:07
  • Are you sure it is necessary in the ID and not just in the NAME attribute??? As I can see from google.com search page they just use the name attribute and a custom id. Commented Feb 21, 2017 at 14:10
  • note: the syntax globalSearch.q.value.length gets the element by its name, not id Commented Feb 21, 2017 at 14:11
  • why two form!!, you can style the same form for mobile devices. Commented Feb 21, 2017 at 14:47

2 Answers 2

1

You probably want to change the placeholder, so the user don't have to delete the text than type in a query. Please view updated function.

function validateSearch() {
  var q = document.getElementById('q');
  if (q.value.length == 0) {
    q.setAttribute('placeholder', 'Enter search term')
    q.style.borderColor = "red";
    return false;
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Two elements can not share same ID. Either use CSS styling to make different looks in mobile, either hide one of forms from webserver (PHP/etc) side either dont use getElementById - instead, use jQuery:

<form name="globalSearch" ... >
<input name="q" data-input-type="desktop" id="q">
..
</form>
<script>
 function validateSearch() {

     var field = $("input[data-input-type="desktop"]');
     field.val("Enter value here...");
     field.css("color","red");
 }
 </script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.