views:

30

answers:

1

Ok, so Im totally new to javascript, but I would need to do following:

on my page I have tabs (done with javascript) and then I have search form. Now I need from those tabs a id to put to that form.

I have tabs1 tabs2 tabs3 and all those href="#1", href="#2" and so on. I would need to get that number out of that tab to form input value. So if use clicks Tab2, it would automatically put "2" as a value in one of my inputs of my form. How to do that? Thanks!

+1  A: 

Explaination in HTML comments:

<!-- retrieves the value from href, skipping the first character -->
<a href="#1" onclick="document.forms.form_name.field_name.value=this.hash.substr(1)">Tab1</a>
<form name="form_name" action="" method="post">
<!-- defaults to tab 1 -->
<input type="hidden" name="field_name" value="1" />

</form>
Lekensteyn
`getAttribute` doesn't work properly in IE, you'll get the resolved URL same as if you used `this.href`. So you'd end up with `ttp://something/#2`. `getAttribute`/`setAttribute` should almost never be used in HTML document; here, I suggest using `this.hash.slice(1)`.
bobince
I used getAttribute to get the *real* attribute value. This works in Firefox, but as you pointed out, it doesn't work in IE. Nice solution, using Link.hash :) Could you explain why you've chosen for `.slice(1)` instead of `.substr(1)`?
Lekensteyn