Solved

default value of a hidden input based on a url?

Posted on 2014-10-16
5
91 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 51

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

758 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

24 Experts available now in Live!

Get 1:1 Help Now