Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

<a> tag onclick

Status
Not open for further replies.

rachelason

Programmer
Jun 28, 2004
114
0
0
GB
Hi Everyone. i am trying to change the value of a textbox with

<form>
<a href="/sss/same.html" onclick="this.form.myt1.value=1">
<input type="text" name="myt1">
</form>
Is this possible to do? Because <a> tag when it reloads the same page ..onclick is not executed. WHen i try a submit button instead of <a> tag,, the value is reset. Any idea as to how else i can change the value? I want the <a> to do it?
cheers
Rach
 

Then either remove the href, or add "return(false);" to the end of the onclick.

I cannot work out why you want to set a form field right before exiting the page, however. And if you don't want to exit the page, why bother with an href at all?

Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
Hi Dan,
Thanks for the reply. The purpose of my page is to display users property in the way they would like to see it.
The page contains
1.A to Z alphabets. (A B C D.....Z)
2.a text box and a search button(<input type="text" value="searchfield"><input type="submit" value="Search">
3.a select box<select name="proplink"><option> getting the value according the above 2 optino you choose.
4. Hidden field set to 1 or 2 according to the above option.<input type="hidden" name="hiddenfield">

If i type something in the textbox and click on 'search'...... the 'hiddenfield' value will be set to 1 and while reloading the page...select box will display properties with the text containing what i typed inthe 'searchfield'. If i click on A ..B or Z... it reloads the page and gets the database with properties starting with the actual letter.
So the obvious problem i am having now with the <a> tag is, i am not able to change the 'hiddenfield value' to 0 and the field still remember the old value which is 1 and displays properties according to that..which is wrong.

I don't know if there is a better way of doing is. Everything is working fine except that with the <a> tag i am not able to display the right list of properties.
Any suggestion?
Rach
 
<a href=".." onclick="forms[0].myt1.value=1">click here</a>

You could use onmousedown="" instead of onclick="" to see the change occur before it loads the next page.

----------
Memoria mihi benigna erit qui eam perscribam
 

Great to see you posting again Stormbind. A minor caveat on that code:

Code:
onclick="forms[0].myt1.value=1"

It assumes that this is the first form on the page. The poster has indicated that this might be the case (but we know that the goal posts change every minute here - and he didn't specifically say there was only one form).

If you use the form name, this will get rid of any (future) ambiguity:

Code:
onclick="forms['myFormName'].elements['myt1'].value=1"

Cheers,
Jeff
 
Hi...i tired different ways to change the value of the field ..used a javascript function from onclick aswell href and anything i can image.but it doesn't change the value..here is the code..
I am using HTMLOS ,but obviously it's not a problem with HTMLOS. ..i am failing to understand how /why the javascript doesn't work with <a> tag.
the name of the page is propertylist.html.


in a for loop......
'<a href="/propertylist.html" class="menu" name="stse" value="alphastr[acol,1]" onclick="this.form.hidse.value=0" >alphastr[acol,1]</a> end of for loop.

alphastr contains array of A to Z.
stse will tell the database what letter i have clicked.


this code doesn't change the value of the textboxfield. Any other suggstion?

 
Hi THere is only one form and i am calling the texbox field with the name.. so i guess no confusion in that...
I get the feeling that there is something wrong in what i am doing.
rach
 
I always had problems with this.form so I always name my forms and use the same to refer to it:

<form name="form1">
<input type="text" name="myt1">
<a href="javascript:void(0)" onclick="document.form1.myt1.value=1">Click Here</a>
</form>

Works for me.
 
rachelason,

I assure you that the code posted does change the value in the given text field.

There may be something else - that we cannot see - on your page which is causing a problem.

----------
Memoria mihi benigna erit qui eam perscribam
 
hi ! you are absolutely right. The value changes this time. I have always used this.form...worked with everything but not with <a> tag i guess. When i click on href,, i can see the value changes to 0 and then goes back to where it was. Atleast i know now it does changes. I believe it's because the page is reloading it remember the old value. Now i have to find a way of changing this. Thanks to everyone who helped me to see the problem.If you come across any idea of remembering the hiddenfield value after the page reloads... i'll be so grateful. Have been working on this the whole morning.
rach.
 
Use document.cookie or ?QUERY_STRING to send data from one page to another.

----------
Memoria mihi benigna erit qui eam perscribam
 

Server-side coding (and submit the form instead of simply reloading a page), or cookies would be the way to go.

Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
Rach,

Just to point out, if you have an href attribute in an anchor element, and you do not have any JS to block it, the page given in the href attribute will be loaded - nothing will be saved at all.

This is standard HTML behaviour. Just in case you weren't aware of that.

Dan

[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 

Check out the online examples at Google and learn about FORMS. You can then look for examples on using location.href to split the href when a GET method is used for sending the form.

Why not write a description (fully) of what you are trying to achieve (don't try and explain how you want to solve it... explain what you want to achieve with the solution). I think part of the problem here is that you are asking questions (that we are answering) without being aware that these are the wrong questions to ask for the solution you are trying to create.

Take a step back, write up a full description of the solution... and post it here! At the very least we can offer some direction! It just seems a lot m ore constructive than this "back-and-forth" detective game we seem to be playing at the moment.

Jeff

 
hi! just to want to let you know i am calling the same page from <a href> . I tired this
onclick="document.propselect.hiddenfield.value=0;this.form.submit();" ...again the value changes to 0 and then back to 1. not sure if i can do it..but doesn't seem to work.
Let me see if i can crack it with the querystring.
rach.
 

Add return false to your onclick:

Code:
onclick="document.propselect.hiddenfield.value=0;this.form.submit();return false;"

Jeff
 
Thanks Dan again,, i wanted to know if <a href> tag behaves as the way you described. I am not very good at javascript so wasn't sure if it's possible or not. It would make things easy for everyone if i could post the code but the problem is it's full of HTMLOS tag which i am not sure how many are familiar with and dont' want to confuse anymore.

what i want to acheive is tell the page there are two different way of getting info from the database. 1.all properties starting with a letter. 2. All properties containing the letter/letters.

I am usign a hidden field to differentiate the two options.
Don't know if there is a better way of doing this.
Hope that explains what i want to acheive. Btw, i tried with the returnfalse in the onclick...same...changes and then chages again.
rach


 
Hi Everyone, thanks to all of you for your great support. I skipped the <a> href as i was enlightened by Dan that <a tag doesn't remember any value on refresh as it doesn't submit. So i have used a submit button from a-z in a for loop and i have used a for loop to deduct which button is click and i am setting the value of hiddenfield over there. Atlast i could find a different solution to what i want to do in the page.
Thanks again everyone.
Cheers
Rach
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top