user3806955
user3806955

Reputation: 77

jQuery Equivalent to getElementbyName not working

I've been trying to create a hidden element and I want to use the value of the hidden element as a condition for if statement.

 var x = document.createElement('input');
        x.type = 'hidden';
        x.value = "Success";
        x.name = "eventName";
        console.log($("input[name='eventName']".value));

In another method,

if ($("input[name='eventName']".value) == "Success") 
{
   ---------------
}

$("input[name='eventName']".value) is returning 'undefined'.I want to get the hidden element value in if statement.

Please help!thanks!

Upvotes: 0

Views: 90

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

$(selector/element) returns a jQuery object so you need to call the .val() method

if ($("input[name='eventName']").val() == "Success") 
{
   ---------------
}

In your case you are calling "input[name='eventName']".value, ie you are trying to access the value property of a string input[name='eventName'] which is undefined and then is passing that to jQuery.


Also your console.log($("input[name='eventName']".value)); won't print the value even if you corrected the syntax to console.log($("input[name='eventName']").value);, because since you are using a selector to find the element and you have not added the newly created element to the dom the selector engine won't be able to find the element.

You can either pass the dom element reference you created like

console.log($(x).value);

or you can run that particular script after the element x is added to the dom.

Upvotes: 2

Related Questions