Solved

default value of a hidden input based on a url?

Posted on 2014-10-16
5
93 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 52

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JavaScript waiting 14 68
Timerpicker in javascript 4 35
Javascript closure 1 34
Javascript create file issue 4 47
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to create an extensible mechanism for linked drop downs.
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…

911 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

20 Experts available now in Live!

Get 1:1 Help Now