Scott Selby
Scott Selby

Reputation: 9570

updating text with jquery in asp.net mvc view

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

Upvotes: 0

Views: 182

Answers (1)

darshanags
darshanags

Reputation: 2519

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/

Upvotes: 3

Related Questions