0

This is strange. My .val() and .html() is always undefined although i am using it after everything is loaded on page. also, .text() is empty.

<div class="row col-md-12">
<div class="row col-md-12">
    <label id="lblDir" style="width:auto">Scripts Directory Path: <input type="text" id="txturl"></label>
</div>
<div class="row col-md-12">
    <div id="grid" style="border:dashed"></div>
</div>
<div class="row col-md-12">
    <input id="btnRun" type="button" value="Run Scripts" style="margin:5px" />
</div>
<div class="row col-md-12">
    <label id="result"></label>
</div>
</div>
<script>
$(function () {
    $("#btnRun").kendoButton();
    $("#btnRun").on("click", function (e) {
        alert($('#txtUrl').text());
    });
});

Things i have tried 1. putting script tag before div tag 2. adding onclick function in button html

<input id="btnRun" type="button" value="Run Scripts" onclick="onInstall();" style="margin:5px" />

Result is the same

Samra
  • 1,815
  • 4
  • 35
  • 71

2 Answers2

3

As @Carsten Løvbo Andersen mentioned, you need to use $('#txturl').val() instead of $('#txtUrl').text() because:

  • #txtUrl doesn't exist. CSS classes and jQuery selectors are often case sensetive. (Case-insenstivity is not very well supported.)
  • input elements don't have .text(), but rather .val().

$(function () {
    $("#btnRun").on("click", function (e) {
        alert($('#txturl').val());
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row col-md-12">
<div class="row col-md-12">
    <label id="lblDir" style="width:auto">Scripts Directory Path: <input type="text" id="txturl"></label>
</div>
<div class="row col-md-12">
    <div id="grid" style="border:dashed"></div>
</div>
<div class="row col-md-12">
    <input id="btnRun" type="button" value="Run Scripts" style="margin:5px" />
</div>
<div class="row col-md-12">
    <label id="result"></label>
</div>
</div>
Chris Happy
  • 7,088
  • 2
  • 22
  • 49
  • 1
    I appreciate the honor, but i'm happy with letting your answer stay, it's well documented. +1 – Carsten Løvbo Andersen Oct 20 '17 at 06:15
  • @Chris Thanks, i had tried .val() previously and even that didn't work. Kousik pointed rightly that I didn't notice txtUrl was upper case :( so STUPID of me...but your point also stands valid :) – Samra Oct 22 '17 at 22:45
1

You have to keep the id in HTML markup and id value in script same. And the id value is case sensitive.

In your html markup the id of input type is "txturl" but in js it is "txtUrl". Make both either "txturl" or "txtUrl" and then it should work fine.

KOUSIK MANDAL
  • 2,002
  • 1
  • 21
  • 46