0

I'm trying to get the value of an input field in my .php document, and with this, set another input field, by triggering an OnClick event.

This is my current code

echo" <label onClick=\" document.getElementById(\"hiddenDescriptionField\").value = 'document.getElementById(\"descriptionField\").value'; ";

How can I set my hiddenDescriptionField from descriptionField using javascript?

4
  • Oh, i already tryed like this: document.validateForm.hiddenDescriptionField.value = 'document.descriptionField.value' Nothing happened too :S Commented Apr 10, 2013 at 10:33
  • Why are you echoing that? There are no php variables in it, so there is no point. End the php ?> and do this in regular html Commented Apr 10, 2013 at 10:35
  • Have you tried removing the single quote from the value? - document.getElementById(\"descriptionField\").value Commented Apr 10, 2013 at 10:35
  • Hello Jakobhans and Jeff Shaver :). I appreciate your fast answers, but: I prefer to echoing a full line of code simply in php instead using opening <?php and closing ?> tags, and mixing tons of languages :). I already resolved my problem. I had one form that didn't possess any "name" property and i needed that for getting the value. Once again, thank you very much for your fast response :) Kind regards, Sam :) Commented Apr 10, 2013 at 11:02

4 Answers 4

2

This should do it:

echo "<label onclick=\"document.getElementById('hiddenDescriptionField').value = document.getElementById('descriptionField').value; \"";

It should be 'onclick' not 'onClick', also your quotes were a bit mangled - I think I've fixed it.

Sign up to request clarification or add additional context in comments.

4 Comments

Hello codebox. I didn't downvote your answer. I never do that and i don't know who did that :S. I appreciate your fast answer, and i already tried your solution, and works fine. About the quotes, the reason they were mangled was because i edited them when i posted the question :P. Once again, thank you for the help :) Kind regards
Just fyi, onclick="javascript:stuff()" the javascript: is a label which is never referenced and might as well be onclick="this_label_does_nothing:stuff()". What it doesn't do is specify the language of the following script, which is js by default. I suggest onclick="stuff()"
And no it wasn't me either, but I suspect that may be why.
Removed from answer, the topic is covered well here: stackoverflow.com/questions/23217/…
2

I'd recommend taking making the event listener non-inline (assuming you add an id to your <label>):

$my_script = <<<HIDDEN_DESC
<script>

var hidden = document.getElementById('hiddenDescriptionField'),
    description = document.getElementById('descriptionField');

document.getElementById('my-label').addEventListener('click', function (e) {
  hidden.value = description.value;
}, false);

</script>
HIDDEN_DESC;

(Put here as a heredoc to fit with your current method of building HTML).

1 Comment

Hello Bokonic. I appreciate your fast answer, and the advice of not making the event non-inline, but i really need it inline :). Your solution didn't fit well on my objectives, but i appreciate your effort and i'll upvote your help :) Kind regards, Sam
1

Since this:

document.validateForm.hiddenDescriptionField.value = 'document.descriptionField.value'

isn't working either (you're telling JavaScript to set the value to the string between the quotes), the problem is probably in the naming of your HTML elements. Is the name of you hiddenDescriptionField correct?

Besides this, you can also check out codebox's answer above, JavaScript is caseSensitive, so onclick won't work. To test that, add a

alert('test');

to your code, and you will easily see if your code is executed or not.

1 Comment

Hello Borniet. I appreciate your fast answer, and i re-checked the naming of my components and i missed one name of one input, and it was causing the failure of getting the value :S. About the caseSensitivity of javascript, Aptana studio is responsible of putting the names correctly on my page, but i will pay attention to that detail from now on ;). Kind regards, Sam
1

In HTML, this should work:

<form>
  <input id="hiddenDescriptionField"/>
  <input id="descriptionField" value="My Field Value"/>
</form>

<label onclick="document.getElementById('hiddenDescriptionField').value = document.getElementById('descriptionField').value" >Click me</label>

You can try this code here:

http://jsfiddle.net/m5XyA/

I suggest that you use JQuery for accessing the DOM.

1 Comment

Hello Mourad. Thank you for your fast response. I also tried your code, and works fine to me :). But i can only give one toggle "OK" :(. But i thank you very much for your fast response :). Happy coding ;). Sam

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.