1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

set value in a form input field using javascript

Discussion in 'Programming' started by Luca tall, Jan 16, 2014.

  1. #1
    How would you set the value in a form input field with javascript. I tried by my own but it is not working. can anyone correct it.

    Incorrect Coding:

    <script type="text/javascript">
    var val = document.getElementById("text");
    val.value = "some input";
    </script>

    <input type="text" id="text">
     
    Luca tall, Jan 16, 2014 IP
  2. LayerNode

    LayerNode Member

    Messages:
    44
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    43
    #2
    Try this :)
    Or:
     
    LayerNode, Jan 16, 2014 IP
  3. Luca tall

    Luca tall Member

    Messages:
    31
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    28
    #3
    Thanks for your reply. Now it is working.
     
    Luca tall, Jan 16, 2014 IP
  4. LayerNode

    LayerNode Member

    Messages:
    44
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    43
    #4
    As you have seen in the demo, the text field named “mytext” has a default value of 12 when the form loads, which can be changed either by directly entering the value in the text input field, or by adjusting the value through the two javascript buttons labeled “+” or “-” .
     
    LayerNode, Jan 16, 2014 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #5
    Might help if you understood why it works. In your original you were running the script before the element existed on the DOM -- source order is VERY important when it comes to manipulating elements as they are run inline with the DOM being built.

    A good rule of thumb is to wait until right before </body> to run your scripts so the complete DOM is built before you start trying to manipulate things.
     
    deathshadow, Jan 16, 2014 IP
  6. Nick Voss

    Nick Voss Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #6
    The following code works perfectly to auto fill drop down and linking functionality popping up a new tab and focusing it and everything. BUT, where I cannot get the text in the box to reset or set to anything. I followed your example, but it just doesn't seem to work. Any ideas? It just ends up leaving the link in the text box rather than the text I specify.

    <script type="text/javascript">
    function newTab(theLocation){
    var tab=window.open(theLocation,'_blank');
    tab.focus();
    }

    jQuery(document).ready(function()
    {

    $('#mainsearch').autocomplete({
    source: function(request, response) {
    $.ajax({
    url: "/_search.php",
    dataType: "json",
    data: {
    term : request.term
    },
    success: function(data) {
    response(data);
    }
    });
    },
    select: function( event, ui ) { newTab(ui.item.value); document.getElementById("mainsearch").value='adsf'; },
    minLength: 2,
    delay: 1000
    });
    });
    </script>

    <input id="mainsearch" type="text" placeholder=" Search..." />
     
    Nick Voss, Dec 4, 2014 IP