Friday, April 29, 2011

Get text box value in javascript

Hi

i get the value in javascript from text box. but, value is not coming after white space

For example,

 job : <input type="text" name="txtJob" value="software engineer">

if i take this value in javascript, i am getting only "software". i am using script like this:

var jobValue = document.getElementById('txtJob').value

how to get full value ?. pls clarify?

  • Gnaniyar Zubair
From stackoverflow
  • Set the id attribute of the input to txtJob. Your browser is acting quirky when you call getElementById.

  • Your element does not have an ID but just a name. So you could either use getElementsByName() method to get a list of all elements with this name:

    var jobValue = document.getElementsByName('txtJob')[0].value  // first element in DOM  (index 0) with name="txtJob"
    

    Or you assign an ID to the element:

    <input type="text" name="txtJob" id="txtJob" value="software engineer">
    
  • +1 Gumbo: ‘id’ is the easiest way to access page elements. IE (pre version 8) will return things with a matching ‘name’ if it can't find anything with the given ID, but this is a bug.

    i am getting only "software".

    id-vs-name won't affect this; I suspect what's happened is that (contrary to the example code) you've forgotten to quote your ‘value’ attribute:

    <input type="text" name="txtJob" value=software engineer>
    
    Gnaniyar Zubair : i thank to all for immediate response. - Gnaniyar zubair
  • If it is in a form

    then it would be

    <form name="jojo">
    

    Job :

    then you would say in javascript
    

    var val= document.jojo.jobtitle.value

    document.formname.elementname

0 comments:

Post a Comment