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!

petteri
  • 11
  • 1

1 Answers1

1

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
  • 64,486
  • 22
  • 159
  • 192
  • 2
    `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 Sep 01 '10 at 17:15
  • 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 Sep 01 '10 at 17:17
  • @Lekensteyn see http://stackoverflow.com/questions/2243824/what-is-the-difference-between-string-slice-and-string-substring-in-javascript – David Kolar Oct 25 '11 at 15:10