[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 506
  • Last Modified:

Remove Hash(#) using javascript and write hidden fields

Hi Experts, just needed some help on carrying out the below:

Strip hashes off of the below variable (user_id), and separate the values and place them in a hidden input field using a loop for each of the values using document.write. Placing the values in the "Name" and "Value" fields as below:


var user_id= "1##2##3##4##5##6##7##8##9##10";
 
<input type="hidden" name="1" value="1">
 
<input type="hidden" name="2" value="2">
 
<input type="hidden" name="3" value="3">
 
<input type="hidden" name="4" value="4">
 
<input type="hidden" name="5" value="5">
 
<input type="hidden" name="6" value="6">
 
<input type="hidden" name="7" value="7">
 
<input type="hidden" name="8" value="8">
 
<input type="hidden" name="9" value="9">
 
<input type="hidden" name="10" value="10">

Open in new window

0
agulaid
Asked:
agulaid
  • 3
  • 2
  • 2
1 Solution
 
Lolly-InkCommented:
I like to use the String object's match function:
<html>
<script>
   var user_id = "1##2##3##4##5##6##7##8##9##10";
   var a = user_id.match(/\d+/g);
   for (var index = 0; index < a.length; index++)
   {
      document.write("<input type='hidden' name='" + a[index] + "' value='" + a[index] + "'>");
   }
</script>
<body>
   
</body>
</html>

Open in new window

0
 
Lolly-InkCommented:
This is probably better, matching non-hashes:

<html>
<script>
   var user_id = "1##2##3##4##5##6##7##8##9##10";
   var a = user_id.match(/[^#]+/g);
   for (var index = 0; index < a.length; index++)
   {
      document.write("<input type='hidden' name='" + a[index] + "' value='" + a[index] + "'>");
   }
</script>
<body>
   
</body>
</html>

Open in new window

0
 
agulaidAuthor Commented:
I've tested this and it hasn't worked both IE and FF crash.

<html>
 
<script>
 
function abdi() {
   var user_id = "1##2##3##4##5##6##7##8##9##10";
   var a = user_id.match(/[^#]+/g);
   for (var index = 0; index < a.length; index++)
   {
      document.write("<input type='hidden' name='" + a[index] + "' value='" + a[index] + "'>");
   }
 
   }
</script>
 
 
 
<body>
<a href="#" onclick="abdi();">test</a>
</body>
</html>

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
sh0eCommented:

<html>
 
<script>
 
function abdi() {
   var user_id = "1##2##3##4##5##6##7##8##9##10";
   var a = user_id.match(/[^#]+/g);
   document.open();
   for (var index = 0; index < a.length; index++)
   {
      document.write("<input type='hidden' name='" + a[index] + "' value='" + a[index] + "'>");
   }
   document.close();
 
   }
</script>
 
 
 
<body>
<a href="#" onclick="abdi();return false;">test</a>
</body>
</html>

Open in new window

0
 
sh0eCommented:
Don't accept my comment as solution.  Give all points to Lolly-ink.
0
 
agulaidAuthor Commented:
sh0e, test that, the script doesnt write anything.
0
 
agulaidAuthor Commented:
Sorry guys, it works... I totally forgot the field was hidden.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now