Home » Jquery » javascript – how to get the url parameters and set it as the default value of a text box onload

javascript – how to get the url parameters and set it as the default value of a text box onload

Posted by: admin February 22, 2020 Leave a comment

Questions:

URL

http://localhost:8082/EPHI2/SearchOrg.html?orgName=Appollo

I have to extract the parameter from this url and set it as the default value of a text box onload of the page.

My HTML page is as follows.

SearchOrg.html

    <!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<link rel="stylesheet"
    href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
<style>
.topnav {
  overflow: hidden;
  background-color: #FCFEEA;
}

.topnav a {
  float: left;
  color: black;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

.topnav a:hover {
  background-color: #CBF894;
  color: black;
}

.topnav a.active {
  background-color: #4CAF50;
  color: white;
}
</style>
</head>
<body>
<div class="topnav">
        <a href="landing.html">Home</a>
        <a href="search.html">Search Patient</a>
        <a href="manage_policy.html">Manage Policy</a>
        <a href="#" class="xactive">Search Organization</a>
    </div>
    <br>
    <br>
    <br>
    <br>
    <form style="padding: 10px 400px;" name="searchOrganization" action="viewOrganization" method="post">
        <input type="text" name="searchOrg" id="searchOrg" class="form-control"
            placeholder="Search the organization" /> <br>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;<input
            type="submit" class="btn btn-primary" id="Search" value="Search" /> <input
            type="button" class="btn btn-primary" id="ok" value="OK" /> <input
            type="reset" class="btn btn-primary" id="clear" value="Clear" />
    </form>
</body>
</html>

I have to set the default value of the textbox “searchOrg” as the value of the url parameter “orgName”.

How to&Answer:

here is complete code just copy it, thanks

<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
$( window ).on( "load", function() {
  function getParameterByName( name,href )
{
name = name.replace(/[\[]/,"\\[").replace(/[\]]/,"\\]");
var regexS = "[\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( href );
if( results == null )
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));

}

$(‘#searchOrg’).val(getParameterByName(“orgName”,”http://localhost:8082/EPHI2/SearchOrg.html?orgName=Appollo“));
}); //copy till here