0

I am trying to select one input box, But because i may have more than one inputs, i am trying to limit the one i am selecting. Here is my jquery code that select the the input but for some reason it doesn't work. Is theresomething i am doing wrong? This is be executed once the document is ready.

if ( $("input[name='RETURN.URL'][value='http://link.com?TYPE=P&PID=ST-L09F166&CONSTITUENCY=WBST']").length > 0 )
{
    alert("Transcript report");
}
else
{
    alert("Not Transcript Report");
}

Here the html

<input type="hidden" name="RETURN.URL" value="http://link.com?TYPE=P&amp;PID=ST-L09F166&amp;CONSTITUENCY=WBST">
<input type="hidden" name="SUBMIT_OPTIONS" value="">
5
  • 1
    drop the [value] qualifier... It's probably broken. Also: add a class and use that as the selector $('input.report'); Commented Aug 20, 2013 at 15:49
  • I don't understand your problem. What is the purpose of your code? Can you have the same name several times? Error message? Commented Aug 20, 2013 at 15:52
  • @Richard: In that case an ID is more appropriate if he wants to target a specific input. Commented Aug 20, 2013 at 15:54
  • @glautrou Sure, ID would work too. Just anything except for "value". Value shouldn't be used as a CSS Selector filter. Commented Aug 20, 2013 at 15:55
  • 1
    I would agree, that an id would be awsome, however i don't actually generate the html has the html page is generated dynamically, in which i have no control on. Commented Aug 20, 2013 at 16:00

5 Answers 5

5

Your selector is using the HTML escaped attribute value:

[value='http://link.com?TYPE=P&amp;PID=ST-L09F166&amp;CONSTITUENCY=WBST']

The actual value of the selector should use & and not &amp;:

[value='http://link.com?TYPE=P&PID=ST-L09F166&CONSTITUENCY=WBST']

The value was HTML escaped so that the value would be correctly represented within the attribute, but the query selector needs to match by the actual value.

You'd probably be better off using a simple selector such as a class. This is particularly important, as the URL represented in the [value] attribute could have its query string parameters in any order and represent the same resource.

Sign up to request clarification or add additional context in comments.

1 Comment

Dammit, you beat me by 1 minute. Feel free to use this fiddle if you want to.
1

HTML entities are translated before they're shown in the browser, therefor &amp; becomes & and so forth. So when you're filtering the selector you're looking for something that's been translated into something else.

Here's the JSFiddle.

Without getting into details of why you're selecting input elements this way, you just need to understand what I said and change the selector to:

$("input[name='RETURN.URL'][value='http://link.com?TYPE=P&PID=ST-L09F166&CONSTITUENCY=WBST']")

1 Comment

Wow thank you, this is what i was looking into, and it totally make sense, I agree filtering the input that i am looking might not be the best, but i don't really generate the html unless i use jquery.
1

use jQuery attr() method

if($("input[value='http://link.com?TYPE=P&PID=ST-L09F166&CONSTITUENCY=WBST']").attr('name') == 'RETURN.URL'){
 alert("Transcript report");
}
else
{
    alert("Not Transcript Report");
}

Unless otherwise, these contents are dynamically added, it is better to give this input an id, and access via it.

3 Comments

How would it set the value? cause i really don't want to set the anything
@Richard: I have actually edited with a modified use of attr() method. My initial answer would indeed set the value.
@Jseb: I have edited my answer with a modified use of attr() method. This should work.
0

lose the dot in RETURN.URL (its not a preferred way of adding names with dots to input elements)

& use $('input[name=RETURNURL]').val().length

Comments

0

Check the value instead of using it as a selector.

JSFiddle

if ( $("input[name='RETURN.URL']").val().length > 0 )
{
    alert("Transcript report");
}
else
{
    alert("Not Transcript Report");
}

2 Comments

Please don't abuse the <kbd> element, it's meant for marking up user input (such as keyboard buttons), not for making your link stand out.
Come on, folks. Let's not get into a fight over a simple formatting element. It doesn't really matter which way it's used here, so just let it go.

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.