Solved

default value of a hidden input based on a url?

Posted on 2014-10-16
5
94 Views
Last Modified: 2014-10-17
Hi,

I need to have a default value of "6" for a hidden field that is called "recipientId" if there is no parameter in the url.

It appears that I am trying to do this twice but I dont know which one will actually work.

Here is what I have

Thanks for the help

<script>
	$(document).ready(function () {	
			    $("#Coverage a:not, #ForMembers a:not,#GenInfo a:not(.button)").click(function () {						
	var v = $(this).attr("id");
			$("#recipientId ").val(v);	
			var lhash = $(this).attr("href").substring(1);
			if(lhash==""){
				lhash="6"
			}
			
	         $("div.active").removeClass("active"); 
			  $("div a.active").removeClass("active"); 
			$("a[href='#"+lhash+"']").closest("div").addClass("active");
			$("a[href='#"+lhash+"']").closest("div a").addClass("active");
			$('.label_copy').hide();
			$('#div'+lhash).show();
			if($("#recipientId").val()==6 || $(" #recipientId").val()==7 || $("#recipientId").val()==8) {
				$("#contactForm").show();
			}
			else {
				$("#contactForm").hide ();
			}	
	         $("div.active").removeClass("active"); 
			  $("div a.active").removeClass("active"); 
			$("a[href='#"+lhash+"']").closest("div").addClass("active");
			$("a[href='#"+lhash+"']").closest("div a").addClass("active");
			$('.label_copy').hide();
			$('#div'+lhash).show();

			if($("#recipientId").val()==6 || $(" #recipientId").val()==7 || $("#recipientId").val()==8) {
				$("#contactForm").show();
			}
			else {
				$("#contactForm").hide ();
			}			
			$("div a.active").addclass("active");
			$("div.active").removeClass("active"); 
			$("div.active").removeClass("active"); 
			$("div a.active").removeClass("active"); 
			if (recipID !== '') {
			$("#div" + recipID).show();
			$("#" + recipID).addClass("active");// do you mean that does'nt work
			$("#" + recipID).parent("div").addClass("active");// do you mean that does'nt work
		}
else {
   			$("#div6").show();
 			$("#6").addClass("active"); // please not 6 is not a valid id
			$("#6").parent("div").addClass("active"); // please not 6 is not a valid id
}
	
			$('.hide').click(function () {
				$('.label_copy').hide();
			});

		});
          $("div.active").removeClass("active");
		url = window.location.search.substring(1);
		urlvars = url.split('?');
		var recipID = '';
		for (i = 0; i < urlvars.length; i++){  
			param = urlvars[i].split('=');  

			if (param[0] == "subjectId"){  
				$("#recipientId").val(param[1]) 
				recipID = param[1];
			}  
		}
		if (recipID == '') {
		recipID="6"
		}
		if (recipID !== '') {
			$("#div" + recipID).show();
			$("#" + recipID).addClass("active");
			$("#" + recipID).parent("div").addClass("active");
			var subjectId = new RegExp('[\?&]subjectId=([^&#]*)').exec(location.href).pop();
var recipientId = new RegExp('[\?&]recipientId=([^&#]*)').exec(location.href).pop();
var isSubject = subjectId.length > 0;
var isRecipent = recipientId.length > 0;
		}
	});
</script>

Open in new window

0
Comment
Question by:smfmetro10
5 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 40385592
You have the following twice in the code  - both identical
	         $("div.active").removeClass("active"); 
			  $("div a.active").removeClass("active"); 
			$("a[href='#"+lhash+"']").closest("div").addClass("active");
			$("a[href='#"+lhash+"']").closest("div a").addClass("active");
			$('.label_copy').hide();
			$('#div'+lhash).show();
			if($("#recipientId").val()==6 || $(" #recipientId").val()==7 || $("#recipientId").val()==8) {
				$("#contactForm").show();
			}
			else {
				$("#contactForm").hide ();
			}

Open in new window


In fact you have other code that is repeating.
0
 
LVL 54

Expert Comment

by:Julian Hansen
ID: 40386154
In addition to what Gary said. The code is basically spaghetti - repeats, illogical use of instructions use of undeclared variables.
Take a look at these lines below
	$("div a.active").addclass("active");    // ADDING CLASS HERE
	$("div.active").removeClass("active"); 
	$("div.active").removeClass("active");   // DUPE OF ABOVE
	$("div a.active").removeClass("active");  // REMOVING IT HERE
        // recipID USED BUT NOT DECLARED
	if (recipID !== '') {

Open in new window


To best help you we would need to know what it is you are trying to do - describe it in words.
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 40386205
I suspect this:
			if (param[0] == "subjectId"){  
				$("#recipientId").val(param[1]) 
				recipID = param[1];
			}  

Open in new window

should have subjectId changed to recipientId, like this, if you want it to work:
			if (param[0] == "recipientId"){  
				$("#recipientId").val(param[1]) 
				recipID = param[1];
			}  

Open in new window

0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 40386209
The regular expression may work too. I'd advise sticking some debugging in various points in your code, eg
console.log("Recip id in position 3: "+recipId);

Open in new window

and look at your browser console, then you'll easily see what's working.
0
 

Author Closing Comment

by:smfmetro10
ID: 40387514
Thanks Gary
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

777 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