0

I have a form and want to pass a value from it to my javascript - and use this value to create a javascript variable.

Im a newbie at javascript and cant get it to work.

Here is my code:

<input id="bartype" name="bartype" type="text" value="<?php echo $bartype; ?>" style="display:none" />

I want to grab the value and in my javascript create this var:

var type = $('#bartype');
var targetUrl = 'listing.php?page=$'+type;

How do I do this?

Thanks a lot

2
  • google "jQuery value" and first result is your answer. Why don't try manual first? Commented Aug 26, 2012 at 17:33
  • 1
    JavaScript works client-side; the PHP is irrelevant, please -when asking JavaScript questions- post the client-side (relevant) HTML, not the server-side script that generates it. Commented Aug 26, 2012 at 17:34

1 Answer 1

3
var type = $('#bartype').val();
var targetUrl = 'listing.php?page='+type;

Use google developer tools or firebug to debug javascript

console.log($('#bartype'))

You should go trough some tutorials:

http://docs.jquery.com/Tutorials

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

3 Comments

Actually its not working as I want.. When I inspect the HTML the form part looks right: <input id="bartype" name="bartype" type="text" value="vip" style="display:none"> But the javascript is not getting the type. If I hardcode the type it works: var targetUrl = 'listing.php?page=vip'; But not this: var targetUrl = 'listing.php?page='+type; Any ideas?
$(function() { var type = $('#bartype').val(); var targetUrl = 'listing.php?page='+type; console.log($('#bartype').length) })
You should wrap all Your jQuery code inside this code $(function() { }) it is shortcut for api.jquery.com/ready

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.