?
Solved

Remove Hash(#) using javascript and write hidden fields

Posted on 2008-10-15
7
Medium Priority
?
502 Views
Last Modified: 2012-05-05
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
Comment
Question by:agulaid
[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
  • 3
  • 2
  • 2
7 Comments
 
LVL 16

Expert Comment

by:Lolly-Ink
ID: 22727449
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
 
LVL 16

Accepted Solution

by:
Lolly-Ink earned 2000 total points
ID: 22727457
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
 

Author Comment

by:agulaid
ID: 22727571
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 16

Expert Comment

by:sh0e
ID: 22727792

<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
 
LVL 16

Expert Comment

by:sh0e
ID: 22727798
Don't accept my comment as solution.  Give all points to Lolly-ink.
0
 

Author Comment

by:agulaid
ID: 22727821
sh0e, test that, the script doesnt write anything.
0
 

Author Comment

by:agulaid
ID: 22727881
Sorry guys, it works... I totally forgot the field was hidden.
0

Featured Post

Introducing Priority Question

Increase expert visibility of your issues by participating in Priority Question, our latest feature for Premium and Team Account holders. Adjust the priority of your question to get emergent issues in front of subject-matter experts for help when you need it most.

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
Suggested Courses

741 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