Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

JavaScript DOM Get Value from Form

Posted on 2011-10-04
4
Medium Priority
?
353 Views
Last Modified: 2012-05-12
Ok, here is the problem that I have right now. I want to submit form via AJAX and display fancy message, without need to go to other pages or reload. Which is working fine, except for 1 thing.

When I attempt to get values from form, the issue is with <select>:
<select name="type" id="type"><option value="1">Network Promotion</option><option selected value="2">Regular Promotion</option></select>

Open in new window


Ive tried with:
var type=encodeURIComponent(document.getElementById("type").value)

Open in new window

But it always outputs 0, and no value of selected index.

Ive also tried gettin DOM without value and than:
type.options[type.selectedIndex].value

Open in new window


But that just crashes the script...


So please, can anyone help me out and give me cross-browser solution that works?
0
Comment
Question by:GVNPublic123
  • 2
4 Comments
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 36908535
can you try to alert the following?

alert(document.getElementById("type").innerHTML)
alert(document.getElementById("type").value);

0
 
LVL 15

Expert Comment

by:Eyal
ID: 36908536
$('select#type option[selected]').val()
0
 
LVL 40

Accepted Solution

by:
Gurvinder Pal Singh earned 2000 total points
ID: 36908539
just check if there is any other element called 'type' in your DOM
or
try renaming it to 'type1'
0
 

Author Comment

by:GVNPublic123
ID: 36908558
gurvinder I guess type was too generic. Now it works.

I hope this is cross-browser however, I have a legacy tool on site as well, just in case if ajax doesnt work for someone AND notification for javascript if its not installed/enabled.

0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Integration Management Part 2
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question