473,387 Members | 1,534 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

repeatedly append a javascript string

110 100+
I've got some code which allows users to vote for or against items on my site. However, this is a bit special because the I need to save the users previous votes to a single javascript variable.

The javascript code I've been using is here

Expand|Select|Wrap|Line Numbers
  1. var searchStrNew = "";
  2. function getSearchStrVote(puid, rate, searchStrNew) {
  3.  
  4.     if (rate == '1'){
  5.     searchStrNew = searchStrNew+"&like="+id;
  6.     }
  7.     if (rate =='0') {
  8.     searchStrNew = searchStrNew+"&hate="+id;
  9.     }    
  10. }
  11.  
each time I hit the button which launches this function, it returns only the most recent input.
I need to keep track of past inputs locally (as it is user specific, and I have no need to store it in the db.

So if the user selects like, hate, like for items 1,2,3 respectively, the var searchStrNew should equal &hate=1&like=2&hate=3.
The code I have above only returns the last vote.

I'm sure this is super simple, but my searches have come up empty, and I think it should work as I have it (of course, I've tried minor changes ad still can't get it going).
Oct 22 '07 #1
2 2149
Ferris
101 100+
the function you wrote can only add one "&like" or "&hate" by one calling,and searchStrNew is a globle various. so you needn't pass it to the function as a parameter.

I wrote a simple code for you..I wish it will help you.


[HTML]
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<script language="javascript">
<!--
var searchStrNew = "" ;
function getSearchStrVote()
{
searchStrNew = "" ;
for (var i=1 ; i<4 ; i++)
{
if (document.getElementsByName("item"+i)[0].checked)
searchStrNew = searchStrNew + "&like=" + i;
else
searchStrNew = searchStrNew + "&hate=" + i;
}
alert (searchStrNew);
}
-->
</script>
<body>
<h1>item1</h1>
<input type="radio" id="1" name="item1" checked />like
<input type="radio" id="1" name="item1" />hate
<h1>item2</h1>
<input type="radio" id="2" name="item2" checked />like
<input type="radio" id="2" name="item2" />hate
<h1>item2</h1>
<input type="radio" id="3" name="item3" checked />like
<input type="radio" id="3" name="item3" />hate
<br /><br />
<input type="button" id="btnTest" value="Test" onClick="getSearchStrVote();" />
</body>
</html>


[/HTML]
Oct 22 '07 #2
pedalpete
110 100+
thanks Ferris,

That wasn't exactly what I was looking for, but I think you pointed me in the right direction. At least I have it working now.

My input is not a form, but a collection of buttons which the user can select if they choose , but they don't have to select anything. The result creates a url to retrieve new data via ajax.

the code that I have working is below. Hopefully it will help somebody else later, though I'm not sure how efficient it is.

where start is the original unvoted request url
Expand|Select|Wrap|Line Numbers
  1. var searchStr = start;
  2.  
  3.  
  4. function getSearchStrVote(id, rate) {
  5.     var searchStrNew = searchStr;
  6.     if (rate == '1'){
  7.     searchStrNew = searchStrNew+"&more="+id;
  8.     }
  9.     if (rate =='0') {
  10.     searchStrNew = searchStrNew+"&less="+id;
  11.     }    
  12.     searchStr = searchStrNew;
  13.     alert(searchStrNew, puid);
  14. }
  15.  
thanks for your help.
Pete
Oct 22 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: brian | last post by:
user input accepted via a input in the form. this value needs to be appended to the current url (explained below) ie say present url is www.xyz.com and the user is asked to enter his string and...
4
by: yaffa | last post by:
dear folks, i'm trying to append a semicolon to my addr string and am using the syntax below. for some reason the added on of the ; doesn't work. when i print it out later on it only shows the...
3
by: Jonathan Buckland | last post by:
Can someone give me an example how to append data without having to load the complete XML file. Is this possible? Jonathan
3
by: barry | last post by:
I have the following: function(NAMED) { sString.Append("document.forms.NAMED.style.visibility = 'hidden'") } and would like to convert the NAMED into the object it represents - for example in...
13
by: Richard Shewmaker | last post by:
I've only recently returned to (trying to) doing simple JS, so this is probably a really lame question. I have a Web page with a series of graphics in it (window A). Clicking on a graphic in A...
1
by: Iulian Ilea | last post by:
Hello, Let's say that I have a very big string to write into a file. If I concatenate strings and write the string into file at the end of processing than memory use will increase exponential....
3
by: Meendar | last post by:
Hi, I just want to append the arguments from the function in javascript to the variables as function a() { var temp=arguments;
10
by: HYRY | last post by:
I have the following questions, I am using Python 2.4.2 19167152 #1 11306608 #1 1. the address of a.append and list.append is different, can I get the address of...
3
by: lukethegooner | last post by:
I can't see the wood for the tree's here, I have some code set up to run through a bunch of delete queries, re-append fresh information to local DB tables then run a report. But in the code below...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.