Javascript indexOf

I am not good at javascript, so I have a problem with the following script. I need to check if the name is also contained in the message.

<input type="hidden" id="Message" value="<%= rsDetail.Fields("Message") %>">
<input type="hidden" id="FirstName" value="<%= rsDetail.Fields("FirstName")%>">

<script type="text/javascript">
<!--
function NameCheck(){
var FirstName=document.getElementByID('FirstName');
var CardMessage=document.getElementByID('Message');
var aPosition = CardMessage.indexOf('FirstName');

if (aPosition == -1)
alert("Name Not In Message.");
}
-->
</script>

<a href="NextPage.asp" onClick="NameCheck();">Proceed</a>
+5
source share
6 answers

It seems you are trying to get the input value FirstName. getElementById()returns only node. Instead, access its value:

var FirstName = document.getElementById('FirstName').value;
var CardMessage = document.getElementById('Message').value;

// Then use the variable `FirstName` instead of the quoted string
var aPosition = CardMessage.indexOf(FirstName);

// Best practice would be to use === for strict type comarison here...
if (aPosition === -1)
  alert("Name Not In Message.");
}

Also, note that you have a mistake getElementByIdwith capital Dat the end, where it should be lowercase.

+8
source

'FirstName'with a quote is a string, not a variable FirstName. You need:

// remove the quote, pass the variable FirstName instead of string 'FirstName'
var aPosition = CardMessage.indexOf(FirstName);

EDIT: . node, - D. , :

var FirstName = document.getElementById('FirstName').value;
var aPosition = CardMessage.indexOf(FirstName);
+1

This is what you are trying, I think.

var FirstName=document.getElementById('FirstName').value;
var CardMessage=document.getElementById('Message').value;
var aPosition = CardMessage.indexOf( FirstName );
+1
source

The best way to use jQuery. Your code can be reduced to max. 2 lines:

$("#click").click(function() {
    var found = $('#Message').val().indexOf($("#FirstName").val());
    console.log(found);
});
+1
source

document.getElementByID it should be document.getElementByID

0
source

Try:

var FirstName=document.getElementByID('FirstName');
var aPosition = CardMessage.indexOf(FirstName);

In your example, you are looking for the next line FirstName, not the value of a variableFirstName

-1
source

All Articles