2

Well this question might look simillar to questions easily findable with google but for some reason all solutions out there have not worked for me. All I want to do is to check if there is text inside of textarea.

JS snippet:

var PrzedDzier = document.getElementById('ctl00_m_g_87871370_ce46_4f47_b1eb_614d0106535d_ff105_1_ctl00_ctl00_TextField');   
var a = PrzedDzier.val();
if(a == "")
{
  alert('FU');
  result = false;
}

HTML baby:

<textarea dir="none" class="ms-long" title="PDzier" id="ctl00_m_g_87871370_ce46_4f47_b1eb_614d0106535d_ff105_1_ctl00_ctl00_TextField" cols="20" rows="2" name="ctl00$m$g_87871370_ce46_4f47_b1eb_614d0106535d$ff105_1$ctl00$ctl00$TextField"></textarea>

Debugger (Firebug) stops working at var a = PrzedDzier.val(); so I assume it's something wrong with val() method...

3 Answers 3

8

You have retrieved the object as a JS object, not a jQuery object. You need to use the jQuery ID selector to retrieve the jQuery object:

var PrzedDzier = $("#ctl00_m_g_87871370_ce46_4f47_b1eb_614d0106535d_ff105_1_ctl00_ctl00_TextField");
var a = PrzedDzier.val();
if(a == "")
{
  alert('FU');
  result = false;
}

If you would like to access the text using pure JS, simply use the value property:

PrzedDzier = document.getElementById('ctl00_m_g_87871370_ce46_4f47_b1eb_614d0106535d_ff105_1_ctl00_ctl00_TextField');   
var a = PrzedDzier.value;
if(a == "")
{
  alert('FU');
  result = false;
}
Sign up to request clarification or add additional context in comments.

5 Comments

or use innerHTML on JS object for textarea
@AshwiniDhekane, true, but the OP indicated that they wanted to use the .val() method. I'll update the post to show both.
@JamesHill You should use .value because .innerHTML is a complete mess. Both will work, of course.
Not really a JS programmer, thanks alot for the enlightening ;) Works good now. Cheers!
@JamesHill, @AshwiniDhekane, @Amaan: Never use innerHTML for a textarea. It does not stay in sync with the actual content of textarea. value is the only property that works. See jsfiddle.net/5kA52
1

Since you are using jQuery you can directly access the textarea and get the value as below.

$("#ctl00_m_g_87871370_ce46_4f47_b1eb_614d0106535d_ff105_1_ctl00_ctl00_TextField").val();

But usually, it is more error prone to get an element using its runtime ID (if there are easy chances that the element would be moved which would change the runtime ID), otherwise you can very well use the above code.

Comments

1

I believe using strlen is better than comparing with "" because strlen is much faster and "" takes 6 bytes of ram.just letting you know the other way

var j = document.getElementById('idoftextbox').value;
 if (strlen(j) == 0) 
  {
    alert('FU');
    return false;
  }

Comments

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.