0

I have html like this

<div class="container">
   <input type="hidden" class="myID" value="123123123" />
   <div class="firstName"></div>
   <div class="lastName"></div>
   <div class="myImage"></div>
</div>

it is repeated over and over

I want to change the value of div with class ".firstName" that follows a specifc hidden field. I have this value properly stored in a var like this

var myID = @Model.myID;  //this is from c# mvc, but nevermind that - its value is correct
$('input[value="'+ myID + '"]').next(".firstName").text("mynewvalue");

How do I select a hidden input based on it's value with a variable in the selector?? I know that the hidden field is not even being selected , so the code after that - the .next() isnt important , I just need to know how to properly select a hidden field based on its value

I tried

$('input[value="myID"]').
$('input[value="'+ myID + '"]')
$('.myID[value="'+ myID + '"]')
$('[value="'+ myID + '"]')

UPDATE

oh, if I try $('input[value="123123123"]') meaning manually just type a value in there - it works

8
  • Is this after the dom is loaded? Commented Feb 15, 2013 at 4:32
  • yes, if i just do $(".firstName") it will successfully change every one Commented Feb 15, 2013 at 4:33
  • $('input[value='+ myID + ']') ? Commented Feb 15, 2013 at 4:34
  • what is in the variable myID? Commented Feb 15, 2013 at 4:35
  • 1
    do a console.log(myID); and re-confirm it. Commented Feb 15, 2013 at 4:37

1 Answer 1

3

You can select hidden fields and you can select them by matching their attributes and values.

jQuery:

 $(":input[type='hidden'][value='123']");

This fiddle should get you sorted, of course you'll have to adapt it to your code. Docs on multiple attribute selectors : http://api.jquery.com/multiple-attribute-selector/

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

3 Comments

I put into the question if I just type the value in the selector it works
that's what I wanted, i'll accept , it looks though that my problem was not in my selector but in the var - as others suggested , you're code is pretty damn close to what I wrote , but of course- obivously- yours works :)
updated jsfiddle.net/7Y43A/4 - now it is identical to my code , and works , and least that narrows it a little

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.