Solved

default value of a hidden input based on a url?

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
arrays and buttons with user input 2 41
Move inline javascript functions to external .js file in asp.net 12 54
REST call Failing 1 36
display data from multiple ajax calls 9 36
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

738 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