How do you assign a value to a hidden input field based on a url parameter using jquery

Posted on 2014-08-13
Last Modified: 2014-08-13

I have a function that will change the value of a hidden input based on an <a> click.

What I would like to have, is first the hidden field getting a value assigned to it based on a URL parameter.  The URl will have "subjectId=" with a number assigned to it..... ( So, it could be subjectId=7.... and the hidden input (recipientId) will have a value assigned to it when the page loads.

Here is what I have so far.


 <script type="text/javascript">
$(document).ready(function() { 
    $("div a").click(function() {
        var v = $(this).attr("id");
   	if($("#recipientId").val()==6 || $("#recipientId").val()==7 || $("#recipientId").val()==8) {
	else {
		$("#contactForm").hide ();

<input type="hidden" id="recipientId" name="recipientId" value=""  />

Open in new window

Question by:smfmetro10
    LVL 107

    Expert Comment

    by:Ray Paseur
    Do you have a server-side scripting language available such as PHP or ASP, etc.?
    LVL 58

    Expert Comment

    Why not just do this server side?
    It's more complicated using js for this at the client.
    LVL 58

    Accepted Solution

    If you must do it client side then

    url =;  
    urlvars = url.split('&');  
    for (i = 0; i < urlvars.length; i++){  
        param = urlvars[i].split('=');  
        if (param[0] == "subjectId"){  

    Open in new window


    Author Comment

    Yeah... The server side language is idoc script.... which I can't figure out :)

    Author Closing Comment

    Once again you have saved me!!!

    Featured Post

    Hire Freelancers to Complete JavaScript Projects

    Source the talented Expert Exchange community
    for top quality work on your JavaScript projects.

    Hire the best. Collaborate easily. Get quality work.

    Join & Write a Comment

    The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
    This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now