Solved

default value of a hidden input based on a url?

Posted on 2014-10-16
5
97 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 57

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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

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…
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 …
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…

691 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